【发布时间】:2018-04-29 07:34:34
【问题描述】:
您好,我想过滤带有自定义类别字段的帖子。我创建了自定义字段并且它正在工作。
/* Custom Field for Categories.
======================================== */
// Add new term page
function my_taxonomy_add_meta_fields( $taxonomy ) { ?>
<div class="form-field term-group">
<label for="show_category">
<?php _e( 'Show Category', 'codilight-lite' ); ?> <input type="checkbox" id="show_category" name="show_category" value="yes" />
</label>
</div><?php
}
add_action( 'category_add_form_fields', 'my_taxonomy_add_meta_fields', 10, 2 );
// Edit term page
function my_taxonomy_edit_meta_fields( $term, $taxonomy ) {
$show_category = get_term_meta( $term->term_id, 'show_category', true ); ?>
<tr class="form-field term-group-wrap">
<th scope="row">
<label for="show_category"><?php _e( 'Show Category', 'codilight-lite' ); ?></label>
</th>
<td>
<input type="checkbox" id="show_category" name="show_category" value="yes" <?php echo ( $show_category ) ? checked( $show_category, 'yes' ) : ''; ?>/>
</td>
</tr><?php
}
add_action( 'category_edit_form_fields', 'my_taxonomy_edit_meta_fields', 10, 2 );
// Save custom meta
function my_taxonomy_save_taxonomy_meta( $term_id, $tag_id ) {
if ( isset( $_POST[ 'show_category' ] ) ) {
update_term_meta( $term_id, 'show_category', 'yes' );
} else {
update_term_meta( $term_id, 'show_category', '' );
}
}
add_action( 'created_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
add_action( 'edited_category', 'my_taxonomy_save_taxonomy_meta', 10, 2 );
现在我想过滤显示类别选择框选中的帖子。我正在尝试这样的查询,但它不起作用
$argswp = array(
'post_type' => "post",
'tax_query' => array( array(
'taxonomy' => 'season',
'field' => 'slug',
'terms' => 'yes'
)),
);
$the_query = new WP_Query($argswp);
我在字段部分尝试了 slug 或 id,但它仍然无法正常工作。我该如何解决这个问题?
【问题讨论】:
标签: php wordpress custom-taxonomy