• Resolved charlie67p

    (@charlie67p)


    Hi,

    Sorry I need help again with the Meta Query Builder

    I link each Post event (1 Post = 1 Event) to a CPT named Place, with an ACF field type Post object named place_id from where I can get the Place CPT id

    Now on each Place CPT single, I would like to query all the events that will happen in this place.

    Using the Extended Filters & Sorting > Filter By Meta Queries with the Carousel query loop :
    I use Data type Defaut + Compare operation equal + Field key place_id + …? Here in the field value I would like to get the current CPT id.

    I don’t find the way to do it…

    Thank you for help

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Phi Phan

    (@mr2p)

    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.

    Thread Starter charlie67p

    (@charlie67p)

    It works like a charm

    Thank you – such a great plugin!

    Thread Starter charlie67p

    (@charlie67p)

    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.

    Plugin Author Phi Phan

    (@mr2p)

    @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.

    Thread Starter charlie67p

    (@charlie67p)

    Ok thanks, I’m a bit lost, I’ll try again later with a clear mind ?? . I’ll let you know.

    Thread Starter charlie67p

    (@charlie67p)

    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 .

    Plugin Author Phi Phan

    (@mr2p)

    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.

    Thread Starter charlie67p

    (@charlie67p)

    thank you @mr2p

    I opened a Technical Support ticket, so I mark this topic as Resolved

    Plugin Author Phi Phan

    (@mr2p)

    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);
    Thread Starter charlie67p

    (@charlie67p)

    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!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Get current Post Id and use it in Meta Query Builder’ is closed to new replies.