WordPress how to sort posts according to Custom Field

I tried all solutions given on this site or other websites but did not find success in sorting posts according to custom field payment package. I tried below code

$args = array( 'meta_key'=>'et_payment_package','meta_key'=>693 );

// Variable to call WP_Query.

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :

    // Start the Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
        the_title();
        the_excerpt();
    // End the Loop
    endwhile;
else:

// If no posts match this query, output this text.
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;

wp_reset_postdata();

It shows

Read More

Sorry, no posts matched your criteria.

Related posts

1 comment

  1. Your query arguments are wrong. You have meta_key twice. Try with

    $args = array( 'meta_key'=>'et_payment_package', 'meta_value'=>693 );
    

    And try to specify the post type.

    For instance:

    $args = array(
        'post_type'  => 'my_custom_post_type',
        'meta_key'   => 'et_payment_package',
        'orderby'    => 'meta_value_num',
        'order'      => 'DESC',
        'meta_query' => array(
            array(
                'key'     => 'et_payment_package',
                'value'   => '693',
                'compare' => 'IN',
            ),
        ),
    );
    
    $query = new WP_Query( $args );
    

    I don’t have all the details (post type etc), but this is how you’d go about it. Hope it helps.

Comments are closed.