Insert connected post title for current item in a Query Loop
Support › MB Relationships › Insert connected post title for current item in a Query LoopResolved
- This topic has 3 replies, 2 voices, and was last updated 2 years, 3 months ago by
Peter.
-
AuthorPosts
-
January 4, 2023 at 9:33 AM #40115
Olivier
ParticipantHi,
I'm using the awesome GenerateBlocks plugin to create query loops throughout my site, in this case to display a list of events.
My Event CPT is connected to a Provider CPT via a MB relationship.
On my list of events, for each event I would like to display the name of the Provider (post title) connected to it.
I'm not able to control the code of the query directly (GenerateBlocks does it), but here is a code snippet I can use to replace an element from the query into a custom one:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'licensee-name' ) !== false ) { // CSS class 'licensee-name' to be added to the block where the value should be output $myreplace1 = 'placeholder'; // Block content should be set to the word 'placeholder' $myinsert1 = get_post_meta( get_the_ID(), 'event_host'); // custom field value to retrieve $block_content = str_replace( $myreplace1, $myinsert1 , $block_content ); } return $block_content; }, 10, 2 );
Is it possible to modify this snippet to retrieve the Provider 1) name and 2) featured image connected to that Event?
Thank you
January 4, 2023 at 11:52 PM #40121Peter
ModeratorHello,
I'm afraid that this case is beyond our scope of support. We do not support customization for your specific needs. If you have a compatibility case with another plugin, please contact them to get further assistance. Or you can request a custom development here https://metabox.io/contact/
Please read more about the support policy here https://support.metabox.io/topic/support-policy/
January 5, 2023 at 11:19 AM #40125Olivier
ParticipantThanks Peter, but as far as I understand, that function is a normal WordPress function, not specific to GenerateBlocks - or am I wrong?
I'm not understanding why this code is not working:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'licensee-name' ) !== false ) { $myreplace1 = 'placeholder'; $hol_licensee_name = MB_Relationships_API::get_connected( [ 'id' => 'licensee-events', 'to' => get_the_ID(), ] ); $block_content = str_replace( $myreplace1, $hol_licensee_name , $block_content ); } return $block_content; }, 10, 2 );
Can you help?
January 5, 2023 at 8:46 PM #40132Peter
ModeratorHello,
The function MB_Relationships_API::get_connected() returns an array, you have to use the return value as an array, not a string.
I recommend contacting an expert developer WordPress to help you accomplish the task. Without a basic knowledge of coding, you might use the wrong code and break your site later.
Thanks for your understanding and patience.
-
AuthorPosts
- You must be logged in to reply to this topic.