Damon Damon - 1 year ago 30
PHP Question

Sort archive posts based on multiple meta keys.

I have archive posts which display seminars within a particular seminar category.
Some seminars have multiple venues and multiple start dates with respect to the venues. e.g.

seminar A
has
venue London
and start date
5th June 2017
and
venue New York
with start date
12th December 2017
.
Seminar B
has
venue Los Angeles
and start date
12th June 2017
and
Venue Orlando
with start date as
17th December 2017


I can sort the posts based on first start date meta key which is
sub_seminars_0_start_date
. But the order I need is

Seminar A London 5th june 2017
Seminar B Los Angeles 12th June 2017
Seminar A New York 12th December 2017
Seminar B Orlando 17th December 2017


The order I am getting is

Seminar A London 5th june 2017
Seminar A New York 12th December 2017
Seminar B Los Angeles 12th June 2017
Seminar B Orlando 17th December 2017


How can I do this ?

The code I am using in functions.php is

function pre_get_posts_hook($wp_query) {

if( is_admin() ) {

return $wp_query;

}

if ($wp_query->is_main_query() && ( is_category() || is_archive() ))
{
$wp_query->set( 'orderby', 'meta_value' );
$wp_query->set( 'post_status', 'publish' );
$wp_query->set( 'meta_key', 'sub_seminars_0_start_date' );
$wp_query->set( 'order', 'ASC' );
return $wp_query;
}
}

add_filter('pre_get_posts', 'pre_get_posts_hook' );


I am using the advanced custom fields repeater to create these fields.

Answer Source

The answer to this question can be found here How to group ans sort posts in wordpress according to date?

I fixed the issue and shared the code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download