Skip Navigation

[Resolved] Conditional check if there are related posts

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to test whether there are any related posts using a conditional shortcode?

Solution:
You would want to register a custom shortcode that returns how many connected posts there are in the specified relationship, which you can compare to zero in your conditional shortcode.

Sample code for such a shortcode together with how to use it is given below: https://toolset.com/forums/topic/conditional-check-if-there-are-related-posts/#post-1234376

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 5 years, 7 months ago. 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.

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 2 replies, has 2 voices.

Last updated by emmaM-2 5 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#1234193

I have a parent post 'listing' with child post 'relatedproject'. They are connected via the relationship 'listing-related-project'. I am trying to conditionally output some content on the Content Template for Listing where there are no related child posts.

I've tried following these support tickets with no luck:
https://toolset.com/forums/topic/conditional-html-check-if-post-has-children/
https://toolset.com/forums/topic/create-view-for-posts-with-no-related-posts/
https://toolset.com/forums/topic/conditional-output-to-check-for-related-posts/

I've inserted the following function in the functions.php file:

function child_exists_func( $atts ){
$atts = shortcode_atts(
        array(
            'slug' => '',
        ),
        $atts
    );
$child_posts = types_child_posts($atts['slug']);
if ($child_posts) {
return true;
}
add_shortcode( 'child-exists', 'child_exists_func' );
}

I've registered the shortcode [child-exists] in Settings and I've registered the function 'child_exists_func' for use in conditional evaluations in settings.

And I have tried the following shortcodes in my Content Template:

[wpv-conditional if="( '[child_exists_func slug="relatedproject"]' eq '0' )"]tttt[/wpv-conditional]
[wpv-conditional if="( '[child_exists_func slug="listing-related-project"]' eq '0' )"]ssss[/wpv-conditional]
[wpv-conditional if="( '[child-exists slug="listing-related-project"]' eq '0' )"]xxxx[/wpv-conditional]
[wpv-conditional if="( '[child-exists slug="relatedproject"]' eq '0' )"]zzzzz[/wpv-conditional]

None of these shortcodes output anything using either "eq" or "ne". I must be doing something wrong but I just can't work it out...

Please help ?

Emma

#1234376

Nigel
Supporter

Languages: English (English ) Spanish (Español )

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

Hi Emma

I think your problem is that the types_child_posts is an old API function for pre-Types 3 relationships.

You need to use the API function toolset_get_related_posts to test for whether there are child posts or not (https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts)

I've shared the following custom shortcode before which I think you should be able to use:

/**
 * 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'], array(
        	'query_by_role' => $origin,
        	'role_to_return' => 'other',
        	'need_found_rows' => true )
    	);
    	$count = $connections['found_rows'];
  
    }
  
    return $count;
});

It outputs the number of posts related to the current post for a relationship you specify with the "relationship" attribute, e.g.

[connections relationship="project-task"]

If you want to use it in a conditional shortcode (where you test for zero), don't forget to register the connections shortcode first at Toolset > Settings > Front-end Content

#1234973

Thanks Nigel. This worked perfectly, appreciate your excellent support.