繁体   English   中英

如何将WP_Query结果限制为发布的最后X个帖子?

[英]How to limit WP_Query results to last X number of posts published?

我的目标是将查询结果限制为最近发布的X个帖子。

到目前为止,我的查询看起来像这样:

$args = array(
    'post_type' => 'post'
);
$query = new WP_Query( $args );

是否可以使用一个参数,还是应该编写一个自定义SQL查询?

参考: http : //codex.wordpress.org/Class_Reference/WP_Query#Parameters

WP_Query接受的参数之一是posts_per_page 默认情况下,它按最新帖子排序,因此您只需要posts_per_page参数。

<?php
$args = array(
    'post_type'      => 'post',
    'posts_per_page' => '5'
);

$query = new WP_Query( $args );
   Solution :

   $args = array('post_type' => 'post','posts_per_page' => 'your no of post', 'orderby' => 'ID', 'order' => 'DESC');
  $query = new WP_Query( $args );

   AND FOR Custom Query
   global $wpdb;

   $qyery="your custom query here";
   $wpdb->query($query); Use this function 

   or 

   $wpdb->get_results( $query, output_type );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM