I have a CPT which contains one large field group ('Global'), and two other field groups which contain ACF Clone Fields ('NJ' and 'PA'). The fields in NJ and PA are identical to Global, except that they are prefixed with nj_ and pa_.

I also have a separate radio button field, where I specify which is the primary location ('primary_geo') for that particular post. This is effective insofar as it enables me to control the information shown on the post itself.

My problem is this: when I use WP Query to create a list of these posts, I'd like to filter and sort the posts according to the data in the field prefixed with the primary_geo. Meaning sometimes pa_field will be compared with nj_field. But because it's outside the loop, I can't get that value.

Is there a way for me to achieve what I'm looking for?

2 Answers 2


Directly using WP_Query, I don't think this is possible as you would require some logic inside the WP_Query which allowed you to say 'if primary_geo is set to NJ then look in NJ, or if it's set to PA then look in PA'.

However, what you could do is use a hook to update a new single meta value on that post which stores the 'effective' geo field - i.e. the value for whichever location the 'primary_geo' is pointing at. You can achieve that with something like this:

add_action('save_post', 'update_geo_value', 10, 2);

function update_geo_value($postID, $post) {
   $primaryGeo = get_post_meta($postID, 'primary_geo');

   if ($primaryGeo == 'NJ') {
       $georesult = get_post_meta($postID, 'nj_field');
   } elseif ($primaryGeo == 'PA') {
       $georesult = get_post_meta($postID, 'pa_field');
   } else { 
       echo "This should probably never happen. Put some debug logging here!";

   update_post_meta($postId, 'effective_geo_value', $georesult);


Note this is example code and may need correction / changing values or code according to your use variables, etc.

So what this does is move that logic about figuring out which field to use to the point when a post is saved, and then the value 'effective_geo_value' is the one you need which corresponds to either NJ or PA. You can then obviously use effective_geo_value in a WP_Query with the post meta parameters


  • You'd need to update this calculated value every time you update any of the involved meta values for this post! I.e. if the item moved from NJ to PA for some reason, you'd need to update the effective_geo_value.



Not entirely sure this will help you but.. WP_Query does actually allow you to supply a custom field for filtering the output. See this link

So if you knew the name of the custom field in advance (and I can't exactly work out if you can in your example) then you can supply this directly or as a variable passed into the args. Then your query outputs only posts with the supplied custom field.

This might not help with the sorting of the output of course, unless the default sorting options are all you need?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.