0

Trying to list all Custom Post Type titles based on filtered Custom Taxonomy Terms I am getting the list of all post titles instead of getting the list of Queried post.

Here is the code I am using:

<?php $loop = new WP_Query( 
   array( 
        'post_type' => 'photos', 
        'technique' => 'zevar', 
        'post_child' => 0, 
        'posts_per_page' => 10 
   ) 
); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title(); ?> 
<?php endwhile; ?>

As you can see I have a Custom Post Type called "photos" and Custom Taxonomy registered as "technique". Under Taxonomy "technique" I have some terms which one of them is "zevar". Can you please let me know what I am doing wrong here?

1 Answer 1

1
  1. The {tax} => {term} pattern is deprecated as of 3.1 you should be using tax_query, but your code should still work for some undetermined length of time.

  2. post_child is not a parameter that WP_Query accepts. I don't know what you expect that to do but at best it does nothing. This may be breaking you query, but I doubt it. I would expect it to be simply ignored.

What you should be using is this, pretty much straight from the Codex:

$args = array(
    'post_type' => 'photos',
    'tax_query' => array(
        array(
            'taxonomy' => 'technique',
            'field' => 'slug',
            'terms' => 'zevar'
        )
    ),
    'posts_per_page' => 10 
);
$loop = new WP_Query( $args );

That code, plus a readable while loop...

while ( $loop->have_posts() ) {
    $loop->the_post();
    the_title();
}

... does work. I tested it. Of course, you have to have the taxonomy setup correctly and you have to have posts in the post type with the right term.

2
  • s_ha_dum, Thanks for ur great help.it is working but the only thing which I cant' understand is this part field' => 'slug', 'terms' => 'zevar'! why did you chose the 'slug'? does this mean i have to specify my custom taxonomy's slug? once again thanks for your help.
    – Behseini
    Commented Apr 21, 2013 at 2:10
  • No. You specify whether you are identifying the term by either the slug or the ID. See the Codex page for WP_Query
    – s_ha_dum
    Commented Apr 21, 2013 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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