Hello –
I have a custom post type, “industries”, that news posts can select from in a Relationship field called “related_industries”. I have no problem getting a list of related industries on the post page, or anywhere that related_industries exists, or getting the list of posts on the single-industry page with the reverse lookup. But I’d like to be able to filter the results on the post archive page by industry, using a query var. It seems in theory that I should be able to do it the same way as I would on a single-industry page, but it doesn’t seem to affect the query at all. I’ve tried a lot of different variations, too many to list, but I’m starting to wonder if it’s possible (especially since I haven’t seen too many people asking the same question).
This is basically the code – a few things have been simplified posting it here (because I’m trying to do this with multiple CPTs) but this should give you the gist of it. Any idea what I might be missing? Thanks!
function query_by_relationship( $query ) {
if( is_admin() ) {
return $query;
}
if (is_archive()) {
if( isset($_GET['related_industries']) ) {
$meta_query[] = array(
array(
'key' => 'related_industries',
'value' => '"' . $_GET['related_industries'] . '"',
'compare' => 'LIKE',
)
);
$query->set('meta_query', $meta_query);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'query_by_relationship');
There is an issue here
$meta_query[] = ...
should be
$meta_query =
If that does not help, what is in $_GET['related_industries']
, does this only have a single selection or can multiple selections be made?
Well, this is awkward. I had taken that code out of functions.php to try things another way. I pasted back in my code from above, tweaked a few var names as needed, and it worked perfectly fine (whether or not I had the [] in, tried it both ways). So whatever the problem in the code was, it must have been one of the extra bits that I had in there before stripping it down to post on the forum!
Thanks anyway for your response, apparently I just needed a sounding board 😉
meta query allows nested queries, do something like
$meta_query = array(
array(
array(
// actual query here
)
)
)
will work, however, this can cause the query to run slower and you’re better off without the extra nesting if you don’t need it.
Hmmm, spoke too soon. My code now works fine on post archive pages, but I need to add it to the CPT “events” as well, which uses the exact same set of custom fields. But this isn’t working. I can test and see that it is correctly triggering the various if statements, but it doesn’t seem that the meta query gets applied – it just returns all results, unfiltered. Posts are still working, though, with the exact code below. Does this sort of meta query get applied differently to a CPT than to the standard post?
function query_by_relationship( $query ) {
if( is_admin() ) {
return $query;
}
if ((is_archive() && $query->is_main_query()) || ( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'events' ) ) {
if( isset($_GET['sector']) ) {
$meta_query = array(
array(
'key' => 'related_industries',
'value' => '"' . $_GET['sector'] . '"',
'compare' => 'LIKE',
)
);
$query->set('meta_query', $meta_query);
} else if( isset($_GET['pa']) ) {
$meta_query = array(
array(
'key' => 'related_practice_areas',
'value' => '"' . $_GET['pa'] . '"',
'compare' => 'LIKE',
)
);
$query->set('meta_query', $meta_query);
} else if( isset($_GET['lawyer']) ) {
$meta_query = array(
array(
'key' => 'related_people',
'value' => '"' . $_GET['lawyer'] . '"',
'compare' => 'LIKE',
)
);
$query->set('meta_query', $meta_query);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'query_by_relationship');
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.