Get current Post Id and use it in Meta Query Builder

[ad_1]

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!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer