anthonytherockjohnson
anthonytherockjohnson

Reputation: 627

Display and order using two meta keys

Hi I have a custom wordpress page using a query for a meta key if the product is on special for a specific store, however I need to order them on page by a different meta_key called wpcf-order-by.

The following query does not work, it displays my items but not according to the wpcf-order-by.

what am I doing wrong, I have searched and I cannot find anything that suits my case

  $args = array(
              'post_type' => 'product',
              'post_status' => 'publish',
              'meta_key' => 'store_opening',
              'meta_value' => 'yes',
              'orderby' => 'wpcf-order-by',
              'order' => 'asc',
              'posts_per_page' => '-1'
          );

Upvotes: 0

Views: 258

Answers (1)

Elvin Haci
Elvin Haci

Reputation: 3572

You need to use meta_query for that. meta_query fits to cases where you are working with more than one meta keys in your wp query requests.

$args = array(
              'post_type' => 'product',
              'post_status' => 'publish',
              'meta_key' => 'wpcf-order-by',
              'orderby' => 'meta_value',
              'meta_query' => array(array(
                'key'     => 'store_opening',
                'value'   => 'yes',
                 )),
              'order' => 'asc',
              'posts_per_page' => '-1'
          );

If the values of 'wpcf-order-by' are numbers, then set 'orderby' => 'meta_value_num' instead of 'meta_value'.

Upvotes: 1

Related Questions