posts - How to sort WP_Post Object array by object field in php?

admin2025-01-07  3

I have a php code as shown below in which I want to sort WP_Post Object array by object field post_date.

if ( $search ) {
    $area_query = new \WP_Query( [
        's'           => $search,
        'post_type'   => 'abc-xyz',
        'post_status' => 'publish'
    ] );
}   

                    

This is what I have tried in order to sort WP_Post object array but it doesn't seem to work.

if ( $search ) {
    $area_query = new \WP_Query( [
        's'           => $search,
        'post_type'   => 'abc-xyz',
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'ASC',
    ] );
}   

                    

Problem Statement:

I am wondering what changes I should make in the php code above so that it sorts WP_Post object array order by date.

I have a php code as shown below in which I want to sort WP_Post Object array by object field post_date.

if ( $search ) {
    $area_query = new \WP_Query( [
        's'           => $search,
        'post_type'   => 'abc-xyz',
        'post_status' => 'publish'
    ] );
}   

                    

This is what I have tried in order to sort WP_Post object array but it doesn't seem to work.

if ( $search ) {
    $area_query = new \WP_Query( [
        's'           => $search,
        'post_type'   => 'abc-xyz',
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'ASC',
    ] );
}   

                    

Problem Statement:

I am wondering what changes I should make in the php code above so that it sorts WP_Post object array order by date.

Share Improve this question asked Jul 8, 2020 at 16:01 user1950349user1950349 1012 bronze badges 4
  • You shouldn't be directly accessing the posts array in WP_Query, that's not how WP_Query works. If you want an array of WP_Post objects you should use get_posts instead. Also if $search is false then $area_query is undefined. – Tom J Nowell Commented Jul 8, 2020 at 16:10
  • Also, the order argument doesn't sort the object field. Those are query variables, not class variables, they are unconnected, and they have a predefined list of valid values as documented in the official WP_Query document. No sorting of the objects happens in PHP, and the order is the order the rows are returned from the database query. Remember, WP is not an OO framework. – Tom J Nowell Commented Jul 8, 2020 at 16:17
  • I agree, I need to use get_posts instead of WP_Query. I am wondering if you can give me pointer how I can use it. Also, if $search is false then it would go inside the else statement. I haven't posted the complete question. – user1950349 Commented Jul 8, 2020 at 16:18
  • The official documentation will show you how to use get_posts, but keep in mind it has performance costs if used incorrectly. WP_Query is superior. It's also expected that questions are complete and self-contained – Tom J Nowell Commented Jul 8, 2020 at 16:25
Add a comment  | 

1 Answer 1

Reset to default 0

I am wondering what changes I should make in the php code above so that it sorts WP_Post object array order by date.

WordPress/PHP does not sort the objects, that's not how this works. Additionally, the sort argument does not take the name of object fields. The two are not connected. The order of the posts is determined by the results of rows from the post table returned by the SQL query.

Instead, sort is a parameter used to construct an SQL query. If we look at the documentation we can see the valid values, so you are already using the correct order parameters:

$area_query = new \WP_Query( [
    's'           => $search,
    'post_type'   => 'abc-xyz',
    'post_status' => 'publish',
    'orderby'     => 'date',
    'order'       => 'ASC',
] );

The above code is asking the database for posts, srted by date in ascending order ( e.g. 1AD, 2AD, 500AD, 1000AD, 2000AD, etc ). It may be that you wanted the reverse.

Note that if you are storing the data in post meta, this will not work, the date here is the date the post was published.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252822a76.html

最新回复(0)