Skip Navigation

[Gelöst] Conditional Output based on post relationships

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Client wants to use the wpv-conditional shortcode with a test for whether the current post has any connected posts from some relationship.

Solution:
That requires registering a custom shortcode that will return the number of connected posts, like so:

/**
 * Register connections shortcode
 *
 * <a href="https://toolset.com/forums/users/att/" tabindex="79">@att</a> (string) relationship : post relationship slug
 * @return count of connected posts
 */
add_shortcode( 'connections', function( $atts = [] ){
 
    // provide defaults
    $atts = shortcode_atts( 
        array(
            'relationship'      =>   '',
        ), 
        $atts
    );
 
    global $post;
    $count = 0;
 
    $relationship = toolset_get_relationship( $atts['relationship'] );
 
    if ( $relationship ) {
 
        $parent = $relationship['roles']['parent']['types'][0];
        $child = $relationship['roles']['child']['types'][0];
        $type = $post->post_type;
 
        $origin = ( $parent == $type ) ? 'parent' : 'child';
 
        // Get connected posts
        $connections = toolset_get_related_posts( $post->ID, $atts['relationship'], $origin, 9999, 0, array(), 'post_id', 'other', null, 'ASC', true, $count );
 
    }
 
    return $count;
});

You need to specify the relationship slug like so:

[connections relationship='cocktail-spirit']

And the custom shortcode will need registering at Toolset > Settings > Front-end Content to be able to use it in wpv-conditional shortcodes.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

100% of people find this useful.

This support ticket is created vor 4 Jahre, 5 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 Antworten, has 3 Stimmen.

Last updated by ian-scottP vor 4 Jahre, 4 Monate.

Assigned support staff: Nigel.

Author
Artikel
#1096387

I would like to only display some content when there is a connected post through a particular post relationship. In the site, we have spirits related to cocktails, and a view cocktail-recipes-for-a-spirit. In the content template for spirits, I would like to display this view only if there are connected cocktails.

In another thread it was suggested to just leave the No Results message blank, and put everything I need in the view. However, right now, we're calling the Spirt name (ie Cocktails Using Rye Whiskey), which I don't think I could do in the view. I would like to not show that message if there is no connected cocktail.

#1096851

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dave

If the simple way you refer to doesn't suit then you can register a custom shortcode that uses the Toolset Relationships API to test for the existence of connected posts, which you can then use in a conditional statement.

As this is something that comes up often enough, I've written such a shortcode. Add the following code to your functions.php file:

/**
 * Register connections shortcode
 *
 * @att (string) relationship : post relationship slug
 * @return count of connected posts
 */
add_shortcode( 'connections', function( $atts = [] ){

	// provide defaults
	$atts = shortcode_atts( 
		array(
			'relationship'		=>	'',
		), 
		$atts 
	);

	global $post;
	$count = 0;

	$relationship = toolset_get_relationship( $atts['relationship'] );

	if ( $relationship ) {

		$parent = $relationship['roles']['parent']['types'][0];
		$child = $relationship['roles']['child']['types'][0];
		$type = $post->post_type;

		$origin = ( $parent == $type ) ? 'parent' : 'child';

		// Get connected posts
		$connections = toolset_get_related_posts( $post->ID, $atts['relationship'], $origin, 9999, 0, array(), 'post_id', 'other', null, 'ASC', true, $count );

	}

	return $count;
});

You need to specify the slug of the relationship, like so, and it will return the number of posts in that relationship connected to the current post.

[connections relationship='cocktail-spirit']

You can register that shortcode for use in conditional statements so that you can test whether there are any connected posts before trying to output something from them.

See https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#1097038

Thank you. I'll try that, but it seems very complicated for what I'm trying to do. I guess I'd like to suggest a GUI-based way to do this as a feature request, like you can use for conditional output based on other factors.

Dave

#1098473

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dave

Well, I'd like to think that I did the hard work for you, if you add the above code to your site then you just need to

- register the shortcode 'conditions' for use in Toolset shortcode arguments at Toolset > Settings > Front-end Content
- insert the conditional shortcode and what you are testing for using the conditional output button

The UI doesn't have any means of adding shortcode arguments (whether you are inserting Views shortcodes, or custom shortcodes), so you then need to edit the condition to add the slug of the relationship.

In my test site I have a "left-right" relationship to connect Left and Right posts, so I can use it like so:

[wpv-conditional if="( '[connections relationship='left-right']' eq '0' )"]
You have no rights.
[/wpv-conditional]
#1098779

You definitely did the hard work, and I thank you for it. However, it seems like this is a feature that is going to be very useful as post relationships get more and more used (I've already seen several questions about it on this forum) and this is a very unwieldy way to do it.

Thanks,

Dave

#1300685

I'm trying to get this to work on my site but not having success. Does this only work for a traditional parent-child relationship? I have connections that are post to post and neither is the parent.

It's an event website where some sessions have speakers and others do not. I have a view that lists the sessions. I want to conditionally show a link that reads "see speakers" if there are speakers connected to that session.

I've added your code to my functions.php, registered the connections shortcode in toolset settings, and I wrote this condition (and several variations) in the view loop item:

[wpv-conditional if="( '[connections relationship='speaker-schedule-item']' ne '' )"]

I've tried gt '0' and eq '0' and then evaluate as false. Nothing seems to work.

It is doing something as I can see some posts with the links and others without, but it's inaccurate.