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.
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.
WP_Query
, that's not howWP_Query
works. If you want an array ofWP_Post
objects you should useget_posts
instead. Also if$search
is false then$area_query
is undefined. – Tom J Nowell ♦ Commented Jul 8, 2020 at 16:10order
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 officialWP_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:17get_posts
instead ofWP_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:18get_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