26

For some reason I'm finding it a struggle to grab any posts using a custom taxonomy... can anyone unravel my stupidity?

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

query_posts($args); 

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

Taxonomy Declaration:

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
  ),
));
  }

Custom Post Type Declaration:

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

}

37

Firs of all don't use query_posts() ever, read more about it here: When should you use WP_Query vs query_posts() vs get_posts()?.

You have to use WP_Query to fetch posts what you need. Read documentation for it. In your case the query could be like this:

$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
    Just noticed that it seems to pull all posts with the custom post type 'Adverts'. However this seems to do the job: $the_query = new WP_Query( array( 'post_type' => 'Adverts', 'advert_tag' => 'politics' )); – Stephen Feb 7 '13 at 14:21
  • @Stephen {tax} was deprecated since version 3.1 in favor of {tax_query} and {tax_query} was introduced. this still works but we should not use the deprecated functions. tax_query is used with an array of taxonomy queries. i was working on FAQs Custom Post type and it worked for me pretty much the same way as {tax} taxonomy slug argument in WP_Query does. – Aamer Shahzad Nov 19 '15 at 20:56
16

i am using this query to fetch custom posts (FAQs Posts) with its custom taxonomy (faq_category). since {taxonomy} parameter in WP_Query args was deprecated since v.3.1 and introduced {tax_query}. below is the code that works perfectly.

$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();
  • This is the correct answer - the accepted answer will not filter by taxonomy since the tax_query requires an array of arrays. This nested method is essential to getting this to work. Thanks for your answer ) – Tom Dyer Jul 28 '16 at 21:36
  • yes you are right, welcome Tom Dyer – Aamer Shahzad Aug 18 '16 at 21:11
  • Yes, this one also helped me to get the taxonomies template to work. Thank you! – user3135691 Feb 16 '18 at 10:34
  • Hey @AamerShahzad I have the exact same question and I used your answer but the page is pulling no posts. Can you help me out here? stackoverflow.com/questions/55783769/… – Desi Apr 21 at 21:34
-1

This answer now is not valid anymore since wordpress changes their taxonomy parameter information. please use this way. It will work. It works for me. "tax_query" replaces with "tax". hope it will work.

$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();
  • It's the exact opposite - tax was the old way, tax_query is the current (v3.1+) way. – WebElaine Jan 3 at 22:50
  • Well I am working v4.5 and it works with me – mamunuzaman Jan 5 at 12:22
  • WP is famous for being backwards-compatible. The old way still works, but it's been deprecated, so it may eventually be removed and it's safer to use the newer method. – WebElaine Jan 7 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.