0

I'm new at Wordpress development and I was wondering (and trying to solve this) for a long time, so maybe you can help me.

My WP use a custom post type named Products. And there is a custom taxonomy with a lot of terms that describe that products.

i.e:

  • Products (custom post type)
  • ----Product line (custom taxonomy)
  • -----------Cardline (term in custom taxonomy)
  • -----------Boneline (term in custom taxonomy)
  • -----------Odoline (term in custom taxonomy)
  • -----------Gineline (term in custom taxonomy)

So, I was able to list all the terms of my custom taxonomy, and to do a while loop to list all the posts inside each term.

However, I was looking to a while loop to list the posts that are related to only one of my custom taxonomy's terms. For example: I want to list only the "international books".

I've tried a lot of solutions, but I still can't get it. Do you have any idea that may help me? I'm working on this. But I can't get anything.

  <?php

  $args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'line',
            'field' => 'slug',
            'terms' => array( 'cardline' )
        ),
    ),
    'post_type' => 'prod'
);
$query = new WP_Query( $args );

if ( have_posts() ) {

    $term = $query->queried_object;

    while ( have_posts() ) : the_post();
        //Output my posts
        the_title();
        the_content();
    endwhile;
}

//RESET YOUR QUERY VARS
wp_reset_query();
?>

Thanks!

  • 1
    You created a new query and assigned it to $query, but your loop does not output the contents of that query object. Refer to WP_Query: Multiple Loops example for the correct syntax. – Milo May 22 '16 at 16:12
0

You need to use the methods of your query instance, rather than the global functions (which are merely wrappers for the global $wp_query):

if ( $query->have_posts() ) {

    $term = $query->queried_object;

    while ( $query->have_posts() ) : $query->the_post();
        //Output my posts
        the_title();
        the_content();
    endwhile;
}
  • Thank you very much, that works exactly in the way I need it! – Germán Gallo May 22 '16 at 16:55

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.