Looper with Query String and orderby multiple custom ACF fields not working

Hi there,
Have spent hours reading, researching and trying… please help.
eg.


( ‘orderby’ with multiple ‘meta_key’s If you wish to order by two different pieces of postmeta (for example, City first and State second), you need to combine and link your meta query to your orderby array using ‘named meta queries’.)

The data selection part works, but the sorting of the output in the looper is not working.

  1. Have installed code snippet to allow for PHP to generate Shortcode,
  2. Created Page to house the shortcode, to generate the Query String, and copy it into the the Posts Looper provider (type Query String) to check the output.

// Custom Post Type: navigate-activity
// Phase: acf_activity_phase
// Step Number: acf_phase_step_nbr
// Task Number acf_phase_step_task_nbr
// Activity Number acf_step_task_activity_nbr

Want to Loop over specified Phase, Steps, Tasks
and have navigate-activity posts output sorted by
acf_activity_phase, acf_phase_step_nbr (within phase), acf_phase_step_task_nbr (within the Step), acf_step_task_activity_nbr (within Task)

The PHP code is…

$meta_query[] = array(
‘relation’ => ‘AND’,
‘phase’ => array(
‘key’ => ‘acf_activity_phase’,
‘value’ => ‘Discover’
),
‘step’ => array(
‘key’ => ‘acf_phase_step_nbr’,
‘value’ => array(1,2,3,4),
‘type’ => ‘numeric’
),
‘task’ => array(
‘key’ => ‘acf_phase_step_task_nbr’,
‘value’ => array(1,2,3,4),
‘type’ => ‘numeric’
),
‘orderby’ => array(
‘phase’ => ‘ASC’,
‘step’ => ‘ASC’,
‘task’ => ‘ASC’
)
);
$query = array(
‘post_type’ => ‘navigate-activity’,
‘post_status’ => ‘publish’,
‘numberposts’ => -1,
‘offset’ => ‘0’,
‘no_found_rows’ => true,
‘meta_query’ => $meta_query
);

echo( http_build_query( $query ) );

Query String =

post_type=navigate-activity&post_status=publish&numberposts=-1&offset=0&no_found_rows=1&meta_query%5B0%5D%5Brelation%5D=AND&meta_query%5B0%5D%5Bphase%5D%5Bkey%5D=acf_activity_phase&meta_query%5B0%5D%5Bphase%5D%5Bvalue%5D=Discover&meta_query%5B0%5D%5Bstep%5D%5Bkey%5D=acf_phase_step_nbr&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D%5B0%5D=1&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D%5B1%5D=2&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D%5B2%5D=3&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D%5B3%5D=4&meta_query%5B0%5D%5Bstep%5D%5Btype%5D=numeric&meta_query%5B0%5D%5Btask%5D%5Bkey%5D=acf_phase_step_task_nbr&meta_query%5B0%5D%5Btask%5D%5Bvalue%5D%5B0%5D=1&meta_query%5B0%5D%5Btask%5D%5Bvalue%5D%5B1%5D=2&meta_query%5B0%5D%5Btask%5D%5Bvalue%5D%5B2%5D=3&meta_query%5B0%5D%5Btask%5D%5Bvalue%5D%5B3%5D=4&meta_query%5B0%5D%5Btask%5D%5Btype%5D=numeric&meta_query%5B0%5D%5Borderby%5D%5Bphase%5D=ASC&meta_query%5B0%5D%5Borderby%5D%5Bstep%5D=ASC&meta_query%5B0%5D%5Borderby%5D%5Btask%5D=ASC

As you can see the results are NOT ordered by:
acf_activity_phase, then acf_phase_step_nbr, then acf_phase_step_task_nbr

Please please help… what am I doing wrong?

Private note with login provided.

Thanks in Advance
Simon

The next part I want to do after sorting this out, is to have a Component (Posts Looper) with 2 parameters for data selection by

  • Phase
  • Step
    and have these be included in the query_string, rather than manually hardcoding/generating/updating the looper provider query_string each time.

ie. Is this possible? (2 parameters that are used in the parts of query string related to acf_activity_phase (eg.Discover) and acf_phase_step_nbr (eg. 1)). I am ok if the parameters only accept 1 value each rather than an array.

Then I can reuse, and just update the parameter values rather than need to regenerate the query string with the PHP code snippet each time.

Kind Regards
Simon

Hello Simon,

Thank you for the inquiry.

The meta_query above looks invalid. If you check the example in the documentation, you’ll understand what I mean.

https://developer.wordpress.org/reference/classes/wp_meta_query/#comment-1989

$meta_query_args = array(
	'relation' => 'OR', // Optional, defaults to "AND"
	array(
		'key'     => '_my_custom_key',
		'value'   => 'Value I am looking for',
		'compare' => '='
	),
	array(
		'relation' => 'AND',
		array(
			'key'     => '_my_custom_key_2',
			'value'   => 'Value I am looking for 2',
			'compare' => '='
		),
		array(
			'key'     => '_my_custom_key_3',
			'value'   => 'Value I am looking for 3',
			'compare' => '='
		)
	)
);
$meta_query = new WP_Meta_Query( $meta_query_args );

And the orderby parameter should not be included in the meta_query.

https://developer.wordpress.org/reference/classes/wp_meta_query/#comment-4187

$q = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        'state_clause' => array(
            'key' => 'state',
            'value' => 'Wisconsin',
        ),
        'city_clause' => array(
            'key' => 'city',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array( 
        'city_clause' => 'ASC',
        'state_clause' => 'DESC',
    ),
) );

Your next inquiry is a bit unclear. What do you mean by “data selection”? You might want to check out the Dynamic Content > URL > Query String Parameter.

Best regards.

Thanks!
Just needed a new set of eyes looking at it… moving the sort stuff out of the meta query worked. thank you.

Sorry for not being clear with the 2nd part of the query… what I mean is, is this possible.
using ROW element parameters in the ROW looper provider query string.
eg.

So PHP to generate the query string (selecting Custom Post Type with multiple ACF fields and sorting correctly with multiple ACF fields) is:

$meta_query[] = array(
‘relation’ => ‘AND’,
‘phase’ => array(
‘key’ => ‘acf_activity_phase’,
‘value’ => ‘Discover’
),
‘step’ => array(
‘key’ => ‘acf_phase_step_nbr’,
‘value’ => 1,
‘compare’ => ‘=’,
‘type’ => ‘numeric’
),
‘task’ => array(
‘key’ => ‘acf_phase_step_task_nbr’,
‘value’ => 1,
‘compare’ => ‘=’,
‘type’ => ‘numeric’
),
);

$query = array(
‘post_type’ => ‘navigate-activity’,
‘post_status’ => ‘publish’,
‘numberposts’ => -1,
‘offset’ => 0,
‘no_found_rows’ => true,
‘meta_query’ => $meta_query,
‘orderby’ => array(
‘phase’ => ‘ASC’,
‘step’ => ‘ASC’,
‘task’ => ‘ASC’
)
);

So I got it working…

Now I set up element parameters
{
“phase” : {
“label” : “Phase Text (Discover)”,
“type” : “text”,
“initial” : “Discover”
},
“step#” : {
“label” : “Step#”,
“type” : “number”,
“initial” : “1”
} ,
“task#” : {
“label” : “Task#”,
“type” : “number”,
“initial” : “1”
}
}

and now want to use them (eg.{{dc:p:step#}}) in the query string.

eg. correct query string such as:
post_type=navigate-activity&post_status=publish&numberposts=-1&offset=0&no_found_rows=1&meta_query%5B0%5D%5Brelation%5D=AND&meta_query%5B0%5D%5Bphase%5D%5Bkey%5D=acf_activity_phase&meta_query%5B0%5D%5Bphase%5D%5Bvalue%5D={{dc:p:phase}}&meta_query%5B0%5D%5Bstep%5D%5Bkey%5D=acf_phase_step_nbr&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D={{dc:p:step#}}&meta_query%5B0%5D%5Bstep%5D%5Bcompare%5D=%3D&meta_query%5B0%5D%5Bstep%5D%5Btype%5D=numeric&meta_query%5B0%5D%5Btask%5D%5Bkey%5D=acf_phase_step_task_nbr&meta_query%5B0%5D%5Btask%5D%5Bvalue%5D={{dc:p:task#}}&meta_query%5B0%5D%5Btask%5D%5Bcompare%5D=%3D&meta_query%5B0%5D%5Btask%5D%5Btype%5D=numeric&orderby%5Bphase%5D=ASC&orderby%5Bstep%5D=ASC&orderby%5Btask%5D=ASC

Last Question: how to generate this from PHP code. ie. how to be able to use {{dc:p:step#}} in the PHP to generate the above query string using element parameters

ie. instead of
‘step’ => array(
‘key’ => ‘acf_phase_step_nbr’,
‘value’ => 1,
‘compare’ => ‘=’,
‘type’ => ‘numeric’
),

I tried using (in the PHP code)
‘key’ => ‘acf_phase_step_nbr’,
‘value’ => ‘{{dc:p:step#}}’,

to generate the query string using the element parameter as a value… but it then generates

&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D=%7B%7Bdc%3Ap%3Astep%23%7D%7D
instead of
&meta_query%5B0%5D%5Bstep%5D%5Bkey%5D=acf_phase_step_nbr&meta_query%5B0%5D%5Bstep%5D%5Bvalue%5D={{dc:p:step#}} (That I manually added into the query string )

And ideas?
Kind Regards
Simon

Thank you for the update.

In the PHP code, try to temporarily use a placeholder for the dynamic content, such as {{{DC_STEP_NUMBER}}} , and then manually replace it with the actual dynamic content after the query string is generated. It’s probably not working as expected because the code snippet plugin doesn’t recognize or it can’t translate the dynamic content

Best regards.

yep, that what I did. thanks

You aremost welcome, Simon.