如何使用WP_Query显示最近的自定义类别帖子?

时间:2018-06-12 12:40:04

标签: wordpress

我正在使用WordPress网站并面临自定义wp_query的问题,因为我在这里使用tax_query。我想展示具有某些分类的特定术语的帖子。它显示完美的结果,但显示较旧的帖子。我只是想展示这个类别的最新帖子。这怎么可能?是否有任何订单参数,或由某些代码完成。这是我的查询代码结构:

$category = $attr['category_slug'];        
$args=array( 'post_type'        =>'post',
                     'posts_per_page'   =>'1',
                     'post_status'      =>'publish',
                     'tax_query'        => array(
                                        array(
                                            'taxonomy' => 'category',
                                            'field'    => 'slug',
                                            'terms'    => explode(",",$category),
                                            ),
                                        ),
                );

      $query  = new WP_Query($args);

4 个答案:

答案 0 :(得分:2)

关注codex

$category = $attr['category_slug'];        
$args=array( 'post_type'        =>'post',
                     'posts_per_page'   =>'1',
                     'post_status'      =>'publish',
                     'orderby' => 'date',
                     'order'   => 'DESC',
                     'tax_query'        => array(
                                        array(
                                            'taxonomy' => 'category',
                                            'field'    => 'slug',
                                            'terms'    => explode(",",$category),
                                            ),
                                        ),
                );

      $query  = new WP_Query($args);

答案 1 :(得分:1)

您可以添加'order'=> 'DESC'显示最新帖子。

$category = $attr['category_slug'];        
$args=array( 'post_type'        =>'post',
                     'posts_per_page'   =>'1',
                     'post_status'      =>'publish',
                     'order' => 'DESC',
                     'tax_query'        => array(
                                        array(
                                            'taxonomy' => 'category',
                                            'field'    => 'slug',
                                            'terms'    => explode(",",$category),
                                            ),
                                        ),
                );

      $query  = new WP_Query($args);

答案 2 :(得分:0)

即使您没有使用“订单”参数,也必须将“DESC”作为ASCENDING订单的默认值,您必须提及

'order' => 'ASC',

现在代码应该是这样的降序:

$category = $attr['category_slug'];        
$args=array( 'post_type'        =>'post',
                     'posts_per_page'   =>'1',
                     'post_status'      =>'publish',
                     'order'            => 'DESC',
                     'tax_query'        => array(
                                        array(
                                            'taxonomy' => 'category',
                                            'field'    => 'slug',
                                            'terms'    => explode(",",$category),
                                            ),
                                        ),
                );

      $query  = new WP_Query($args);

答案 3 :(得分:0)

您是否尝试根据文档https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters使用order and orderby选项:

Import java.util.Scanner;

Class BIL{
//I am defining a class, a function;

  Public static void main(string args[]){

      Scanner userInput = new Scanner(System.in);


      Double MH, c, Md;
      //I define variable with double-precision 64-bit floating point;

      System.out.println(Enter the load, please:);
      //It prints the argument passed, into the System.out which is the standard output. In this particular case it prints it and goes to a new line;
      MH = userInput.nextDouble();
      //The call to .nextDouble() waits for a value to be input. Once input, the value will be stored in the assigned variable MH20;
      System.out.println(Enter capacity, please:);
      c = userInput.nextDouble();
      Md = MH/c;
      System.out.println(The mass needed is:);
      System.out.println(Md);
      //it prints out the result;
   }
}