0

I am trying to get the most recent post of the taxonomy term 'featured-layout' within the taxonomy 'value_category'. The following code is returning all the 'featured-layouts' when I'm only wanting to return the most recent:

<?php

$cat_arr = array(
  'test-1',
  'test-2',
  'test-3',
);

$args = array(
  'post_type' => 'values',
  'posts_per_page' => 1,
);

$mobile_posts = get_posts($args);
$count = 0;

foreach ($mobile_posts as $mobile_post) : setup_postdata($mobile_post);

  //Getting 'Featured' Value
  $featured_value_cat_query = new WP_Query( array (
    'post_type' => 'values',
    'posts_per_page' => -1,
    'tax_query' => array(
      'relation' => 'AND',
        array(
          'taxonomy'   => 'value_layout_position',
          'field'      => 'slug',
          'terms'      => 'featured-layout',
        ),
        array(
          'taxonomy' => 'value_category',
          'field'    => 'slug',
          'terms'    => $cat_arr,
        ),
    ),
  ) );

  if ( $featured_value_cat_query->have_posts() ) {

    while ( $featured_value_cat_query->have_posts() ): $featured_value_cat_query->the_post();

      echo ' | ' . the_title();

    endwhile;

  }

endforeach;

wp_reset_postdata();

?>

Does anyone know how to only return the most recent post from the 'featured-layout' term? Thanks!

0

Because your posts_per_page has a value of -1 means return all matching query.

You should update your post_per_page to 1 and specify the order, and orderby parameters. Order the posts by date in descending order.

$featured_value_cat_query = new WP_Query( array (
    'post_type' => 'values',
    'posts_per_page' => 1,
    'orderby' => 'date', // 
    'order' => 'DESC'
    ...

You can also learn more about the WP_Query Order and OrderBy Parameters

  • Thanks for the quick response! I did try that but for some reason it only returns one post from 'test-1' when it should be returning posts for 'test-2' and 'test-3'. – bhood Jun 26 '17 at 14:45
  • It only returns one item because of the posts_per_page value set to 1. How do you define recent posts in your blog, within a week, 3 days, or the last 3 posts? – Den Isahac Jun 26 '17 at 14:50
  • In terms of posts_per_page , are you talking about when I use it within $args or $featured_value_cat_query ? – bhood Jun 26 '17 at 15:06
  • What I wanted to know now, is how do you personally define the recent posts? Basically recent posts involves filtration from dates. Does it mean that your recent post could be the posts posted less than a week old? How do you define it? – Den Isahac Jun 26 '17 at 15:09
  • Gotcha. I haven't defined the recent post so I guess it's using whatever the default setting is. I personally define recent post as being the most recent post (not trying to be a smart ass here ;) It's returning recent posts at the beginning of the loop but I only want to return the single most recent post with 'featured-layout' for 'test-1', 'test-2', and 'test-3' – bhood Jun 26 '17 at 15:20

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.