Home › Forums › Front-end Issues › HELP: Filtering by ACF field in Archive
Hi All,
I have a CPT with an ACF group to build case studies (projects). One of the fields in the case study is a checkbox list of ‘disciplines’ pulled from a custom taxonomy.
I can display a full archive (/projects/), and I can display individual projects (/project/my-project-1) without issue.
In the individual project display, I have added a link to the key disciplines to link them to /projects/discipline=Discipline%20Name
on the archive page I have added the following to try and only display projects tagged with the discipline:
// args
if (!$_GET['discipline']) :
$args = array(
'numberposts' => -1,
'post_type' => 'case_study',
);
else :
$args = array(
'numberposts' => -1,
'post_type' => 'case_study',
'meta_query' => array(
'key' => 'discipline',
'value' => $_GET['discipline'],
'compare' => 'IN',
)
);
endif;
var_dump($args);
$the_query = new WP_Query( $args );
// display project list
The $args dump looks correct:
`array(3) {
[“numberposts”]=> int(-1)
[“post_type”]=> string(10) “case_study”
[“meta_query”]=> array(3) {
[“key”]=> string(10) “discipline”
[“value”]=> string(20) “This Discipline”
[“compare”]=> string(2) “IN” }
}
but the query isn’t affected and still displays all projects in the list.
Where am I going wrong?
I don’t need ‘live filtering’, just the link in a single project to be able to show a list of all projects in the same discipline.
Any pointers would be awesome – scoured the main docs, but can’t find any clues.
Ben
UPDATE:
Sorted it.
Needed a tax_query
End result that allowed me to filter archive based on the taxonomy:
// Filter by Taxonomy when needed.
if ($_GET['discipline']) :
$term = get_query_var('discipline');
$args = array(
'post_type' => 'case_study',
'tax_query' => array(
array(
'taxonomy' => 'discipline',
'field' => 'slug',
'terms' => $term,
)
)
);
else :
// show everything
$args = array(
'numberposts' => -1,
'post_type' => 'case_study',
);
endif;
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.