2

Sample Image for sorting reference

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:

Alphabetically sorted

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,

  1. Alphabetically

  2. (Within alphabetically ordered) Yearly

  3. (Within yearly ordered) Numerically

At the same time.

In a peaceful world the sorted posts should look like this

What exactly I wanted

1

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.

  • Thanks Brian. I am gonna follow it up now. I will rephrase my answer as well, to be more clear what I needed. If things work out I will select your answer. – Murshid Ahmed Jun 29 '16 at 4:18
  • I have added some more information regarding what exactly I want, please have a look. Thanks in advance :) – Murshid Ahmed Jun 29 '16 at 5:08
  • @TheDarkKnight have you tried the code I supplied? what is the result you are getting? I replaced custom_key with the name of what I assume is the meta key (the original_act_ordinance_information_0_act_ordinance_number, same as the EXISTS query) so try it now and let me know the result. On looks alone, it should be ordering like you desired. You may have to play around with SQL and work backwards... OR just use $wpdb and circumvent the WP_Query altogether – brianjohnhanna Jun 30 '16 at 14:20
  • Hi. Sorry for the delay. Unfortunately I had give this project to another team member. And I thought I have marked yours as correct answer. Thank you for taking your time :) – Murshid Ahmed Sep 9 '16 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.