Hi @charlie67p,
You cannot do that functionality in CBB, but it provides a custom hook that allows you to adjust any query loop. Here is how to do it:
add_filter( 'cbb_query_loop_block_query_vars', function ( $query_vars, $default_vars, $cbb_params, $query_context, $query_object ) {
// Add code to check if this is the query to alter and on the frontend.
if ( 'filter_places' === ( $cbb_params['metaQuery']['queries'][0]['key'] ?? '' ) && $query_object && $query_object->ID ) {
// Clear the fake filter_places meta.
unset($query_vars['meta_query']);
$query_vars['meta_key'] = 'place_id';
$query_vars['meta_value'] = (array)get_field( 'place_id', $query_object->ID );
$query_vars['meta_compare'] = 'IN';
}
return $query_vars;
}, 10, 5);
In your query loop, add a meta query with the key named filter_places
and set the compare operator as EXISTS
You also need to set bidirectional relationship on your ACF post object field.
I have not tested it myself, but you can get the idea based on above code.
P/S: My other plugin MFB does exactly the same functionality as your case, but it is only available in the Pro version.
Please let me know if it works.
It works like a charm
Thank you – such a great plugin!
Well, sorry, actually it does not work.
I edited my functions.php as you said.
The ACF post object field is set to ACF post object field (I let the Target Field empty, because I don’t know what I should do with it)
Then back to my query loop I did as you said.
…
@charlie67p, You can learn how to set the bidirectional relationship in ACF here: https://www.advancedcustomfields.com/resources/bidirectional-relationships/. You should create a relationship field in the place post type that points to events. You may name it as event_ids and then set it as the target field.
In my code, change from place_id
to event_ids
.
Ok thanks, I’m a bit lost, I’ll try again later with a clear mind 🙂 . I’ll let you know.
OK, the only thing I manage to do (I don’t know if this is what you meant ?) is :
1-When creating an event : to select a place in the list of existing places
+
2-When creating/updating a place : to select an event in the list of existing events.
That works, but it takes time.
The easy way for me would be to have to do only the first step. And then query dynamically the events, filtered by the place Id .
You have to learn about bidirectional relationship. If it works when you manually select it, the only problem is how to set up the bidirectional relationship. If that guide from ACF did not make sense to you, you can look for more tutorials on that topic or open a ticket on the ACF forum for help. If you don’t mind you can open a Technical Support ticket at Dashboard -> Custom Blocks -> Contact Us and provide me with the admin account of your site. I can help you with that.
Phi.
thank you @mr2p
I opened a Technical Support ticket, so I mark this topic as Resolved
The previous snippet code was wrong. The correct snippet code is:
add_filter( 'cbb_query_loop_block_query_vars', function ( $query_vars, $default_vars, $cbb_params, $query_context, $query_object ) {
// Add code to check if this is the query to alter and on the frontend.
if ( 'filter_places' === ( $cbb_params['metaQuery']['queries'][0]['key'] ?? '' ) && $query_object && $query_object->ID ) {
// Clear the fake filter_places meta.
unset($query_vars['meta_query']);
$query_vars['meta_key'] = 'place_id';
$query_vars['meta_value'] = $query_object->ID;
}
return $query_vars;
}, 10, 5);
It works perfectly now. Thank you!
I’m learning now how to use the CBB Advanced Sorting options …
I start to understand the possibilities of combining your CBB and MFB plugins : just amazing!