admin管理员组

文章数量:1319000

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',
                                    ),
                                ),
                        ),
                );

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',
                                    ),
                                ),
                        ),
                );
Share Improve this question asked Oct 16, 2020 at 21:13 PamPam 112 bronze badges 2
  • 1 Check this post - WP_Query - Order results by meta value that you could set the proper args. – RachC Commented Oct 17, 2020 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 Commented Oct 17, 2020 at 1:33
Add a comment  | 

1 Answer 1

Reset to default 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!

本文标签: wp querySort wordpress custom posts based on meta value