0

There seems to be something wrong with the logic of what I'm doing.

I just want to display the latest post in a custom term. But instead I'm displaying every post for that term, even though I am using posts_per_page => 1

I have a taxonomy called "twin" and the term "double portrait", I have 2 posts in a category called "celebrity", both with the term "double portrait". The code I have outputs in the right category fine, but as I say it's outputting for each post instead of just the one.

Here's the code I'm using:

 $post_type = 'post';

  $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );

  foreach( $taxonomies as $taxonomy ) : 

    $terms = get_terms( $taxonomy );

  foreach( $terms as $term ) : 

    $args = array(
      'tax_query' => array(
        array(
          'taxonomy' => $taxonomy,
          'field' => 'slug',
          'terms' => $term->slug
          )
        ),
      'posts_per_page' => 1,
      'ignore_sticky_posts' => true, 
      'orderby' => 'modified',
      );

  $posts = new WP_Query( $args );

  if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
  if(in_category($str)) {
    if(has_term('double portrait','twin')) {
        echo the_excerpt();
    }

  }

  endwhile; endif;

  endforeach;

  endforeach;
3
  • In which template are you using this?
    – jepser
    Aug 19, 2013 at 4:26
  • What is this posts $terms = get_terms( $taxonomy );? That must be a typo?
    – s_ha_dum
    Aug 19, 2013 at 4:35
  • Hi, thanks for the reply. Yes it was a type I had copied over a comment from the code by accident. This is a template part for the index page.
    – UzumakiDev
    Aug 19, 2013 at 9:37

1 Answer 1

0

Your query arguments, formatted to be readable, look like this:

$args = array(
  'taxonomy' => $taxonomy, 
  'term' => $term->slug, 
  'posts_per_page' => 1, 
  'orderby' => 'modified',
  'category' => $str 
);

That does not match any argument pattern I see for WP_Query. I think you are trying to construct a tax_query but that should look like this:

$args = array(
  'tax_query' => array(
    array(
      'taxonomy' => $taxonomy,
      'field' => 'slug',
      'terms' => $term->slug
    )
  ),
  'posts_per_page' => 1,
  'ignore_sticky_posts' => true, 
  'orderby' => 'modified',
  'category' => $str 
);

I am not sure why you have a tax_query and a category argument. Perhaps that is a mistake, perhaps you want an AND or OR relationship between the taxonomies. I am not sure.

I suspect that you also want ignore_sticky_posts so I added that.

3
  • I have a category argument because I want to display posts in the main category of say something like dance, but also with the term landscape or double portrait that I created with a taxonomy. I didn't know about tax_query, thanks, that's cleaned up my argument a lot. I'm still getting more than one post for my query though :/
    – UzumakiDev
    Aug 19, 2013 at 9:42
  • This kind of question is hard to test since I don't have your data in my database. You can roll that category argument into the tax_query though, and I would if were you.
    – s_ha_dum
    Aug 19, 2013 at 13:55
  • Your tax_query worked perfectly.
    – UzumakiDev
    Aug 20, 2013 at 9:46

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.