0

I have posts with TWO custom fields

Data_START : 2018-02-20

Data_END: 2018-02-22

This code make in order all posts by Data_END, and that ok.

add_action( 'pre_get_posts', 'wpse183601_filter_category_query' );
    function wpse183601_filter_category_query( $query ) {
        // only modify front-end category archive pages
        if( is_category() && !is_admin() && $query->is_main_query() ) {
            $query->set( 'posts_per_page','6' );
            $query->set( 'orderby','meta_value' );
            $query->set( 'meta_key','Date_END' );
            $query->set( 'order','DESC' );
        }
    }

But exist one little problem.

If I add a new post with data like this:

Data_START : 2018-02-20

Data_END: 2018-02-19

This new post appear after all sorted posts by Data_END !

Because he have Data_END less previous posts..

So how can I do to make it for first ?

Example: (how it works for this moment)

Loading.....

2018-02-20 - 2018-02-25

2018-02-20 - 2018-02-24

2018-01-20 - 2018-02-23

2018-01-20 - 2018-02-22

It's okay, but not for me !

I want to see for first Data_END LESS like this :

2018-01-20 - 2018-01-22

2018-01-20 - 2018-02-23

2018-02-20 - 2018-02-24

2018-02-20 - 2018-02-25

So.. I can solve this problem, if I set ASC instead DESC, but, all ordered post by Data_END appear in the END of all others posts (in the end of pagination...)

Problem is I don't know how to do this..

  • try order = 'DESC' ? you have DES. if that doesn't work there's a few different ways to specify ordering by meta value so i can go through one of them. – Joel M Feb 18 '18 at 17:36
  • Topic update. pls see it – webstackoverload Feb 18 '18 at 17:52
  • Have a look at the last example on WP_Query Order & Orderby Parameters, scroll down to 'orderby' with multiple 'meta_key's – Milo Feb 19 '18 at 4:59
  • It's a good idea, but it not works.. because post have also different post_date too – webstackoverload Feb 19 '18 at 10:57
  • Your question says nothing about post_date, how is that related and why is it a problem? – Milo Feb 19 '18 at 16:03

Your Answer

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

Browse other questions tagged or ask your own question.