0

I am trying to listing posts from a single category of my custom taxonomy.

I have created a custom post type called "news" and a custom taxonomy called "instituteName".

I then created a number of categories using the "instituteName" taxonomy.

I would now like to display the posts from only a single category. e.g. i have created a category "businessSchool" underneath "instituteName" taxonomy. Now I want to display the four latest post the "businessSchool" category only.

screenshot

Any help would be greatly appreciated!

3 Answers 3

1

@Krzysiek answer above didn't work because have_posts is a method not a property. It should be have_posts() not have_posts. Here's the corrected code:

$my_query = new WP_Query( array(
    'post_type'=>'news',
    'posts_per_page'=>4,
    'tax_query'=>array(
        array(
            'taxonomy'=>'instituteName',
            'field'=>'slug',
            'terms'=>'businessschool'  // change to real slug
        )
     )
) );

while ( $my_query->have_posts() ) {
    $my_query->the_post();
    // display post
}
1

You can do it using custom WP_Query:

$my_query = new WP_Query( array(
    'post_type'=>'news',
    'posts_per_page'=>4,
    'tax_query'=>array(
        array(
            'taxonomy'=>'instituteName',
            'field'=>'slug',
            'terms'=>'businessschool'  // change to real slug
        )
     )
) );

while ( $my_query->have_posts() ) {
    $my_query->the_post();
    // display post
}

PS. This link would be helpful, I guess: http://codex.wordpress.org/Class_Reference/WP_Query

5
  • Thanks for the help, But its not working...:( I have tried "<?php query_posts( array('post_type' => 'news', 'taxonomy'=>'InstituteName', 'category_name' => 'businessSchool', 'showposts' => 4 ) ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>"...
    – Gaurav
    Jul 10, 2013 at 11:16
  • But not working, If i don't add category name, then its working but display all the post underneath "InstituteName" taxonomy.
    – Gaurav
    Jul 10, 2013 at 11:17
  • You shouldn't use 'category_name' - your custom taxonomy IS NOT category. Take a look at my code - it uses tax_query and you should use it... The only thing you have to look out is replacing businessschool with correct term slug. Jul 10, 2013 at 11:46
  • Thanks, Tried the same code, but not working, Even not displaying single post..:(
    – Gaurav
    Jul 10, 2013 at 11:55
  • Are you sure that you modify it correctly? Jul 10, 2013 at 12:41
0
<?php 
  $args = array( 
    'post_type' => 'product',
    'posts_per_page' => -1,
    'product_cat' => 'meditations-intensives', 
    'order' => 'DESC' );
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post(); global $product; 
  endwhile;?>

Your Answer

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

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