【问题标题】:WordPress Category custom fieldsWordPress 类别自定义字段
【发布时间】: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


    【解决方案1】:

    我找到了解决办法

    $seasons = get_categories(
        array(
            'orderby'    => 'name',
            'order'      => 'ASC',
            'hide_empty' => '0',
            'meta_query' => array(
                array(
                    'key'     => 'show_category',
                    'value'   => 'yes',
                    'compare' => '=',
                )
            )
        )
    );
    

    【讨论】:

      【解决方案2】:

      我认为您需要更多地调整选项。特别是术语、分类法和字段

      【讨论】:

      • 对不起,我没听懂。我可以调整什么?
      • 拉动帖子的数组的值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多