问题描述

由于某种原因,我发现使用自定义分类法来抓住任何职位的努力… 任何人都可以解开我的愚蠢?

 $args = array(
    'post_type' => 'adverts',
    'advert_tag' => 'politics' // Doesn't seem to work.
  );

query_posts($args);

while ( have_posts() ) : the_post();
 //Show Posts
endwhile;

分类声明:

add_action( 'init', 'add_custom_taxonomy', 0 );
function add_custom_taxonomy() {
register_taxonomy('advert_tag', 'Adverts', array(
  'hierarchical' => true,
  'labels' => array(
    'name' => _x( 'Advert Tags', 'taxonomy general name' ),
    'singular_name' => _x( 'Advert Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Advert Tags' ),
    'all_items' => __( 'All Advert Tags' ),
    'parent_item' => __( 'Parent Advert Tag' ),
    'parent_item_colon' => __( 'Parent Advert Tag:' ),
    'edit_item' => __( 'Edit Advert Tag' ),
    'update_item' => __( 'Update Advert Tag' ),
    'add_new_item' => __( 'Add New Advert Tag' ),
    'new_item_name' => __( 'New Advert Tag Name' ),
    'menu_name' => __( 'Advert Tags' ),
  ),
  'rewrite' => array(
    'slug' => 'advert-tags',
    'with_front' => false,
    'hierarchical' => true
  ),
));
  }

自定义帖子类型声明:

  add_action( 'init', 'create_post_type' );
  function create_post_type() {
    register_post_type( 'Adverts',
    array(
        'labels' => array(
            'name' => __( 'Adverts' ),
            'singular_name' => __( 'Advert'),
            'add_new' => __( 'Add New' ),
            'add_new_item' => __( 'Add a New Advert' ),
            'edit' => __( 'Edit' ),
            'edit_item' => __( 'Edit Advert' ),
            'new_item' => __( 'New Advert' ),
            'view' => __( 'View' ),
            'view_item' => __( 'View Advert' ),
            'search_items' => __( 'Search Adverts' ),
            'not_found' => __( 'No Adverts found' ),
            'not_found_in_trash' => __( 'No Adverts found in Trash' ),
            ),
        'supports' => array(
                'title',
                'thumbnail',
            ),
        'has_archive' => true,
        'menu_position' => 10,
        'public' => true,
        'rewrite' => array( 'slug' => 'adverts' ),
        'taxonomies' => array('advert_tag')
    )
);

}

最佳解决方案

所有的冷杉都不使用 query_posts(),请阅读更多关于它:When should you use WP_Query vs query_posts() vs get_posts()?

您必须使用 WP_Query 来获取您需要的信息。阅读 documentation 为它。在您的情况下,查询可能是这样的:

$the_query = new WP_Query( array(
    'post_type' => 'Adverts',
    'tax_query' => array(
        array (
            'taxonomy' => 'advert_tag',
            'field' => 'slug',
            'terms' => 'politics',
        )
    ),
) );

while ( $the_query->have_posts() ) :
    $the_query->the_post();
    // Show Posts ...
endwhile;

/* Restore original Post Data
 * NB: Because we are using new WP_Query we aren't stomping on the
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();

次佳解决方案

我正在使用此查询来获取自定义帖子 (常见问题帖子) 与其自定义分类法 (faq_category) 。因为 WP_Query 参数中的 {taxonomy} 参数自 v.3.1 起已被弃用,并引入了 {tax_query} 。以下是完美的代码。

$query = new WP_Query( array(
    'post_type' => 'faqs',          // name of post type.
    'tax_query' => array(
        array(
            'taxonomy' => 'faq_category',   // taxonomy name
            'field' => 'term_id',           // term_id, slug or name
            'terms' => 48,                  // term id, term slug or term name
        )
    )
) );

while ( $query->have_posts() ) : $query->the_post();
    // do stuff here....
endwhile;

/**
 * reset the orignal query
 * we should use this to reset wp_query
 */
wp_reset_query();

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。