In this image,
1. The Black letters are default post titles
2. The number before the ‘of‘ is a custom field.
3. The number after the year is the post’s published year
Now I want to sort this according to title’s alphabetical order. So it will be,
C, D, D, L in this case.
At the same time I want it to be sorted by yearly. So all the posts start in ‘D’ should be sorted by post year.
Also the number’s that come before the ‘of‘ should be sorted in ascending order within that year.
The below code chunk didn’t work
$args = array(
'posts_per_page' => 30,
'category__in' => $idObj,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
'custom_key' => array(
'key' =>
'original_act_ordinance_information_0_act_ordinance_number',
'compare' => 'EXISTS',
'type' => 'NUMERIC',
),
),
'orderby' => array(
'title' => 'ASC',
'post_date' => 'DESC',
'custom_key' => 'ASC',
),
);
Where am I making the mistake?
and is there any solutions to achieve this?
EDIT:
So basically I want this…
So this image is sorted alphabetically, at the same time all the posts should be sorted from latest year to the oldest, meanwhile let’s say there are multiple posts in the same year. IE: 16 of 2016, 23 of 2016 and 18 of 2016. I want them to be ordered in ascending order.
In conclusion, I want the posts to be ordered,
-
Alphabetically
-
(Within alphabetically ordered) Yearly
-
(Within yearly ordered) Numerically
At the same time.
In a peaceful world the sorted posts should look like this
1 Answer
I’m not entirely 100% on the order you are looking to achieve, but I think you need to spend a little more time with the documentation for WP_Query
. An orderby
array takes the name of a column in the wp_posts
table OR meta_value
or meta_value_num
as the key, not the name of the custom field key (custom_key
in your case). The actual name of the custom field is defined in the args array as meta_key
.
If you look at the last two examples under Orderby Parameters in the Codex, you can see how this works and apply it to your situation. This is untested but gives you the general idea:
$args = array(
'posts_per_page' => 30,
'category__in' => $idObj,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'original_act_ordinance_information_0_act_ordinance_number',
'compare' => 'EXISTS',
'type' => 'NUMERIC',
),
),
'orderby' => array(
'title' => 'ASC',
'post_date' => 'DESC',
'meta_value_num' => 'ASC',
),
'meta_key' => 'original_act_ordinance_information_0_act_ordinance_number',
);
You also had your meta_query
set up incorrectly so I suggest you review that part of the WP_Query
docs as well.