skip to Main Content

I have a query in wordpress that looks like this,

 $args = array(  
        'post_type' => 'our-team',
        'post_status' => 'publish',
        'posts_per_page' => -1, 
        'orderby'           => array(
            'date_published' => 'ASC',
        )
    );

I am wanting to order my results by 2 attributes, firstly by date_published and then secondly my a meta value "weight". Weight is a numeric value (1 or 2).

When I change it the query to be,

$args = array(  
        'post_type' => 'our-team',
        'post_status' => 'publish',
        'posts_per_page' => -1, 
        'meta_key' => 'weight',
        'orderby'           => array(
            'date_published' => 'ASC',
            'meta_value' => 'ASC'
        )
    );

When I run this query it only returns posts that have a weight of 1?

2

Answers


  1. $args = array(
        'post_type'       => 'our-team',
        'posts_per_page'  => -1, 
        'post_status'     => 'publish',          
        'meta_key'        => 'weight',
        'orderby'         => array( 'meta_value_num' => 'ASC', 'post_date' => 'ASC' )
    );
    
    Login or Signup to reply.
  2. Use date instead of date_published i think date_published is not the right key. and weight should be in int so better to use meta_value_num instead of meta_value try the below code.

    $args = array(
        'post_type'       => 'our-team',
        'posts_per_page'  => -1, 
        'post_status'     => 'publish',          
        'meta_key'        => 'weight',
        'orderby'         => array( 
            'date'           => 'ASC' 
            'meta_value_num' => 'ASC'
        )
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search