Thanks John,
I see where you’re going. And good caution on having too many filters/Queries.
Using “foreach”, maybe something like this?
// append meta query
$meta_query = array(
'relation' => 'AND',
foreach( $value as $val )
{
array(
'relation' => 'OR',
array(
'key' => $name,
'value' => '"' . $val . '"',
'compare' => 'LIKE',
),
),
});
I went a different route and decided to utilize custom taxonomies, in combination with the Taxonomy field. But still utilizing the Javascript Elliot outlined in the tutorial. Seems to be less intensive query and built into WordPress already.
Thanks again for your response.
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ) return;
// bail early if not main query
// - allows custom code / plugins to continue working
if( !$query->is_main_query() ) return;
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
foreach( $value as $val )
{
$meta_query = [
[
'key' => $name,
'value' => '"' . $val . '"',
'compare' => 'LIKE',
],
];
}
}
// update meta query
$query->set('meta_query', $meta_query);
}
I think I see my issue.
Using Checkboxes and not Radio buttons. So my compare was wrong.
LIKE
As outlined here.
https://support.advancedcustomfields.com/forums/topic/filtering-archives-using-checkbox-custom-fields/
However, now I have a new problem. When filtering by multiple fields, it only takes the last argument in the URL, and isn’t showing multiple posts when multiple checkboxes are selected for one field.
Example.
Size: Small, Large
Color: Black, White
/catalog/?size=small,large&color=black,white
I need to show all that have either Small or Large, AND Black or White.
I’m only getting results that are White (all sizes, no Black).
Any help or direction is appreciated. Thanks for your time.
The plugin Smitxv mentions above : https://github.com/mattkeys/ACF-Conditional-Taxonomy-Rules is exactly what I needed. Allowing conditional based on chosen Taxonomy ID. Maybe that should be added to future release of ACF.
Thanks.
AH!!!
That fixed it! My other sites must of updated after the fix.
Thanks for your help.
Oh Wait… It is the Wrapper Attributes at the bottom.
Nevermind. 🙂
I changed over to Relationship as Post Object and that got me what I needed.
Here is a sample that might help get you started.
$author_id = get_the_author_meta('ID');
$posts = get_field('field1_name', 'user_'. $author_id);
if( $posts ):
foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post);
echo '<a href="' . the_permalink() . '">' . the_title() . '</a>';
the_field('field2_name');
the_excerpt();
endforeach;
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
Maybe this helps someone else.
Changed to Post Object from Post ID.
That fixed it!
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.