1

I'm trying to sort the custom posts based on meta value as priority level1, level2 and level3. I have given a dropdown field in post meta as featured posts, standard posts and basic posts. I want to display them as featured posts on top, then standard posts and then basic posts. I tried with below code but it's not sorting the posts in the order.

Any guidance or advise much appreciated!

Here is my code:

Array(
    'name'          => __( 'Listing Type Options', 'text-domain' ),
    'id'            => 'lising_type_options',
    'type'          => 'select',
    'child_of' => '',
    'options' => array(
        '' => 'Select Listing Type',
        '1' => 'Basic',
        '2' => 'Standard',
        '3' => 'Featured',
    ),
    'desc' => ''
),

if( isset( $lp_type ) && !empty( $lp_type ) ){
     if( $lp_type == '3' ){
          $lp_type = 'Featured';
     } elseif( $lp_type == '2' ){
          $lp_type = 'Standard';
     } elseif( $lp_type == '1' ){
          $lp_type = 'Basic';
     }
} else{
     $lp_type = '';
}

This is the query:

            $args = array(
                    'post_type' => $type,
                    'post_status' => 'publish',
                    'posts_per_page' => $postsonpage,
                    'paged'  => $paged,
                    'post__not_in' =>$ad_campaignsIDS,
                    'tax_query' => $TxQuery,
                    // 'meta_key'   => $MtKey,
                    // 'orderby' => $lporderby,
                    // 'order'   => $lporders,
                    'order'      => 'DESC',
                    'orderby'    => 'meta_value',
                    'meta_query' => array(
                        'relation' => 'OR',
                                $MtKey,
                                array(
                                    'relation' => 'OR',
                                    array( 
                                        'key'     => 'lp_listingpro_options',
                                        'value'   => 'lising_type_options',
                                        'compare' => 'LIKE',
                                    ),
                                    array(
                                        'key'     => 'lp_listingpro_options',
                                        'value'   => 'lising_type_options',
                                        'compare' => 'NOT LIKE',
                                    ),
                                ),
                        ),
                );
  • 1
    Check this post - WP_Query - Order results by meta value that you could set the proper args. – Rach Chen Oct 17 at 1:22
  • The $MtKey after relation => 'OR' is throwing me.... ...what is that and why is it there? I don't see anything in your code that defines that variable and I also don't understand why it's dropped in the middle of your query. – Tony Djukic Oct 17 at 1:33
0

Thanks for the response. Managed to find what the cause was. Its caused because of sticky feature, my posts was not sorted accordingly. So, can anyone suggest me how do I remove saved sticky feature field from the database?

Any help would be much appreciated!

| improve this answer | |

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.