如何使用自定义分类法查询自定义帖子类型?


26

出于某种原因,我发现使用自定义分类法来获取任何帖子都非常困难……有人可以揭开我的愚蠢之谜吗?

 $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')
    )
);

}

Answers:


37

所有的杉杉没有使用query_posts(),阅读更多关于它在这里:当你应该使用WP_Query VS query_posts()VS get_posts()?

您必须使用WP_Query获取所需的信息。阅读文档。在您的情况下,查询可能是这样的:

$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();

2
只是注意到,它似乎拉出了所有自定义帖子类型为“广告”的帖子。但这似乎可以完成工作:$ the_query = new WP_Query(array('post_type'=>'Adverts','advert_tag'=>'politics'));
斯蒂芬,

@Stephen {tax}自3.1版起已被弃用,而以{tax_query}和{tax_query}的形式引入。这仍然有效,但是我们不应该使用不推荐使用的功能。tax_query与一组分类学查询一起使用。我正在使用FAQs Custom Post类型,它对我的​​作用与WP_Query中的{tax}分类标准子句参数几乎相同。
Aamer Shahzad 2015年

16

我正在使用此查询来获取具有其自定义分类法(faq_category)的自定义帖子(FAQ帖子)。由于自v.3.1起不推荐使用WP_Query args中的{taxonomy}参数,并引入了{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();

这是正确的答案-接受的答案将不会按分类法进行过滤,因为tax_query需要数组数组。此嵌套方法对于使其正常工作至关重要。感谢您的回答)
Tom Dyer

是的,您是对的,欢迎Tom Dyer
Aamer Shahzad

是的,这也帮助我使分类模板工作。谢谢!
user3135691 '18

嘿@AamerShahzad我有完全相同的问题,我用了您的答案,但该页面没有发布任何帖子。你能帮我吗?stackoverflow.com/questions/55783769/...
德西

-1

由于wordpress更改了其分类参数信息,因此该答案现在不再有效。请使用这种方式。它将起作用。这个对我有用。“ tax_query”替换为“ tax”。希望它能工作。

$the_query = new WP_Query( array(
    'post_type' => 'Adverts',
    'tax' => 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();

恰恰相反- tax是旧方法,tax_query是当前(v3.1 +)方法。
WebElaine

好吧,我正在使用v4.5,它可以与我一起工作
mamunuzaman

WP以向后兼容而闻名。旧方法仍然有效,但已过时,因此最终可能会删除它,并且使用新方法更安全。
WebElaine
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.