Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

ACF Relationships

  • No, the query loop block.

    Select the query loop block, and I would like to check the parameters you added to it.

    I also noticed that you only have a headline block in the query loop to show post meta > display_compnay, please set its dynamic content source to title instead.

  • https://imgur.com/a/YXPLgvl here is the query loop perms.

    Also, wouldn’t changing the display_company to title just show the title of the most recent post in the post type? That’s not what I am trying to accomplish.

    The reason post meta was used is because it’s supposed to be pulling related post data based on what superstar is being shown, and the relative company they work for.

  • Also, wouldn’t changing the display_company to title just show the title of the most recent post in the post type? That’s not what I am trying to accomplish.

    No, you are using the PHP code to alter the query loop. Please follow my instructions and see if it shows on the front end.

    You do not have other parameters to the query loop block, it’s good.

  • It does show on the front end. The reason I asked if it showed the last post title is because on post where no relation is set, it is showing the title of the last post added to the relation post type. How do I make it so if no relation is set, it doesn’t display anything?

    Thanks for your help!

  • In theory if the relationship field is empty then it should return a null value and not any post IDS for the query loop to display. Which makes that hard to explain.

    You could try this PHP instead:

    
    
    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'display-company-name' ) !== false &&
            ! is_admin()
        ) {
            // get the relate post ID
            $current_post = get_the_ID() ;
            $relationship = get_field( "display_company", $current_post );
            if ($relationship) {
            // Merge the current $query_args with the new args
                return array_merge( $query_args, array(
                    'post__in' => $relationship,
                ) );  
            }
        }
        return $query_args;
    }, 10, 2 );
    

    The only change is the addition of the if ($relationship) condition so it only passes the IDs to the post_in parameter if there relationship has any values

  • Thank you david. I have replaced the code with what you’ve provided above, but it’s still displaying the last post added to the company post type, on profiles that have no company selected. Any clue why?

    https://imgur.com/a/cjXKjKI

    As you can see on the back end, no company is added to Bryan Danielson, but when you go to his single post page it shows a company.

  • Place the Query Loop inside a Container Block.
    And in the block settings > REMOVE CONTAINER CONDITION set this to No Post Meta and choose and in the field add the custom field name. This will remove the container and the loop inside it when there is no value in the relationship field

  • Thanks!! This worked like a charm.

    Would I just use the same code to apply post relations between other post types of course changing the field names etc? That wouldn’t cause any conflicts anywhere would it?

  • You just need to make sure the class and field name in the code is correctly set, and correct class is being applied to the grid of the query loop block. 🙂

  • Thank you, Ying!!

  • No Problem 🙂

Viewing 11 posts - 17 through 27 (of 27 total)
  • You must be logged in to reply to this topic.