skip to Main Content

I have the following code which is working fine and sorting the post based on the custom field assigned to each post using ACF plugin. Here is the code used for that purpose

function my_pre_get_posts( $query ) {


// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
    
    $query->set('order', 'ASC'); 
    $query->set('orderby', 'meta_value');    
    $query->set('meta_key', 'active_inactive');    
    
}

// return
return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');

I have a custom field created using ACF which is called active_inactive and it has 2 values to each post, active and inactive.

The first requirement works fine, where all active posts are showing first in the list and then inactive posts, however, dates are not being sorted correctly, and right after active posts, all the inactive posts are sorted from older date first to the end date at last.

So i need to fix dates as well, so right after the active posts list ends and inactive posts lit start, all those inactive posts should be start from the latest date to the oldest date.

Any help would be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Trying something like that but it is not returning any results due to post_date

    function my_pre_get_posts( $query ) {
    
    if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
        
        $meta_query = array(
            'active_inactive' => array(
                'key' => 'active_inactive',
                'compare' => 'EXISTS'
            ),
            'post_date' => array(
                'key' => 'post_date',
                'compare' => 'EXISTS'
            ),
        );
        $order_by = array(
            'active_inactive' => 'ASC',
            'post_date' => 'DESC'
        );
        
        $query->set( 'meta_query', $meta_query );
        $query->set( 'orderby', $order_by );
        $query->set( 'posts_per_page', -1 );
     }
    }
    add_action( 'pre_get_posts', 'my_pre_get_posts' );
    

  2. 
        function my_pre_get_posts($query)
        {
            if (isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post')
            {
                $meta_query = array(
                    'active_inactive' => array(
                        'key' => 'active_inactive',
                        'compare' => 'EXISTS'
                    ),
                    'modified' => array(
                        'key' => 'modified',
                        'type' => 'date',
                        'compare' => 'EXISTS'
                    ),
                );
                $order_by = array(
                    'active_inactive' => 'ASC',
                    'modified' => 'DESC'
                );
                $query->set('meta_query', $meta_query);
                $query->set('orderby', $order_by);
            }
            else
            {
                $meta_query = array(
                    'active_inactive' => array(
                        'key' => 'active_inactive',
                        'compare' => 'EXISTS'
                    ),
                    'modified' => array(
                        'key' => 'modified',
                        'type' => 'date',
                        'compare' => 'EXISTS'
                    ),
                );
                $order_by = array(
                    'active_inactive' => 'ASC',
                    'modified' => 'DESC'
                );
                $query->set('meta_query', $meta_query);
                $query->set('orderby', $order_by);
            }
        }
    
        add_action('pre_get_posts', 'my_pre_get_posts');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search