Support

Account

Home Forums General Issues Sorting posts by two custom fields

Solved

Sorting posts by two custom fields

  • Each post has custom fields “order_status” and “order_date_end”.
    I have sorted posts by “order_status” in lowest order(ASC).
    And among these sorted (go in groups), sorting by the “order_date_end” field from the largest is impossible(DESC).

    $posts = get_posts( array(
              'numberposts' => -1,
              'orderby'   	=> array(
                      'order_status' => 'ASC',
                      'order_date_end' => 'DESC',
                        ),
              'meta_key' 	=> 'order_status',
              'post_type'  	=> 'order-item',
            ) );

    Maybe someone came across?

  • You cannot sort by multiple fields the way you are attempting to do it. Since order_date_end in not references in the query anywhere WP can’t use it.

    Take a look at WP_Query Custom Fields section, particularly the part about “meta_query” which you need to use to filter or order by multiple fields.

    Then look at the orderby section and the code example titled ‘orderby’ with multiple ‘meta_key’s

    Let me know if you can’t figure it out from there?

  • I changed it like this. It seems to work. If I changed correctly.

    $posts = get_posts( array(
    					'numberposts' 	=> -1,
    					'post_type'  	=> 'order-item',
                        'meta_query'    =>  array(
                            'relation'  =>  'AND',
                            'order_status'  =>  array(
                                'key'       => 'order_status',
                                'value'     =>  array(1,2,3,4,5),
                            ),
                            'order_date_end'    => array(
                                'key'       => 'order_date_end',
                                'compare'   =>  'EXISTS',
                            )
                        ),
                        'orderby'   =>  array(
                            'order_status' =>   "ASC",
                            'order_date_end' =>   "DESC",
                        ),
    				) );
    
  • Not sure about the order status value, but that looks good to me

  • My question is where does this go, and how would I get the sort to carry over to an Elementor archive?

    I have a staff directory page which I would like to sort by department and position (manager, supervisor, staff). So My array would look like below, but I don’t know where to save it or implement it.

    Also, I’m not sure if it would be better to set up my department and position fields as text or use a numeric id. If I was doing this in a database I would select the ID and output the label.

    $posts = get_posts( array(
    		'numberposts' 	=> -1,
    		'post_type'  	=> 'Staff-Member',
                        'meta_query'    =>  array(
                            'relation'  =>  'AND',
                            'department'  =>  array(
                                'key'       => 'department',
                                'value'     =>  array(1,2,3),
                                // 1='Admin', 2='Fiscal', 3='Maintenance'
                            ),
                            'position'    => array(
                                'key'       => 'position',
                                'value'   =>  array(1,2,3)
                                // 1='Manager', 2='Supervisor', 3='Staff'
                            )
                        ),
                        'orderby'   =>  array(
                            'department' =>   "ASC",
                            'position' =>   "ASC",
                        ),
    				) );
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.