自定义分类法的最新帖子

时间:2017-06-26 作者:bhood

我正在尝试获取分类法“value\\u category”中分类法术语“featured layout”的最新帖子。当我只想返回最新的布局时,以下代码将返回所有“特色布局”:

<?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();

?>
有人知道如何只返回“特色版面”术语中的最新帖子吗?谢谢

1 个回复
SO网友:Den Isahac

因为你的posts_per_page 值为-1 表示返回所有匹配查询。

您应该更新post_per_page1 并指定order, 和orderby 参数。按日期按降序排列帖子。

$featured_value_cat_query = new WP_Query( array (
    \'post_type\' => \'values\',
    \'posts_per_page\' => 1,
    \'orderby\' => \'date\', // 
    \'order\' => \'DESC\'
    ...
您还可以了解有关WP\\U查询的更多信息Order and OrderBy Parameters

结束