Skip Navigation

[Resuelto] Setting Conditional Groups Based on Relationships using Types 3.0

This support ticket is created hace 6 años, 6 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Este tema contiene 5 respuestas, tiene 2 mensajes.

Última actualización por aaronM-9 hace 6 años, 6 meses.

Asistido por: Beda.

Autor
Mensajes
#901167

I'm in the process of going through all my forms and updating them to the new method of setting relationships between different post types. I noticed the only reference to relationships when creating a Conditional Group is the legacy relationship field. Can you tell me how I would migrate the following code?

[cred_show_group if="($(_wpcf_belongs_product-listing_id) eq  '' )"  mode='fade-slide']
   ...
[/cred_show_group]

Thanks

#901264

For migrated relationships that code will proceed working as we added a compatibility layer for the new API <> Old API.

For Post relationships set up freshly, in the new Many To Many releases, you cannot reference that hidden custom field anymore.
New relationships do not save that field, as they use a Custom Database table to connect the posts.

There is a new API to GET posts and related content for new relationships:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

The easiest is to use a Custom ShortCode that returns the value (Post ID) you want to compare to or 0 if none.

I assume your above code example is used on a CRED Edit Form for Child posts.
I assume a Many To Many Relationship between 2 post types.
I assume this is NOT a migrated but a NEW relationship, as elsehow this is not needed.

Hence, after setting up your new Many to Many relationship, you can create this Custom ShortCode:

function check_if_have_related_posts(){
        //Get current post object and ID
	global $post;
	$post_id_current = $post->ID;

        //The Post relationship slug to check
        $relationship_to_query = "post_type_one-post_type_two";

        //Set the role the other post type has:
        $role = "child"; //could be parent, child

        //Get related posts with new API, if none, it'll return 0, if any, the ID (hence not 0).
       $related_post = toolset_get_related_post( $post_id_current, $relationship_to_query, $role);

	return 	$related_post;
}
add_shortcode( 'have-related-posts', 'check_if_have_related_posts' );

Now you can check in HTML conditional or CRED Conditionals like this:

[cred_show_group if="([have-related-posts] ne  '0' )"  mode='fade-slide']
    It has related posts
[/cred_show_group]
#901286

Hi Beda,

Thanks for the response. That's very helpful. I made some modifications to your suggestion and it appears to be working. Before closing the ticket, would you mind giving it a quick glance and see if you think there's anything I overlooked? My intention is to be able to pass a post ID and a parent post type slug to check and have it return the parent post ID - this way I have a bit more versatility in where I can use the shortcode.

- Aaron

/* GET PARENT POST ID */

function be_get_parent_id($atts){
  
  	//Get current post object and ID
    global $post;
    $post_id_current = $post->ID;
  
  	$a = shortcode_atts( array(
	  	'postid' => $post_id_current,
        'parent' => '',
    ), $atts );

	//get post type
	$post_id_current = $a['postid'];
  	$post_type_current = get_post_type($post_id_current);
 
    //The Post relationship slug to check
    $relationship_to_query = $a['parent']."_".$post_type_current;
 
    //Set the role the other post type has:
    $role = "parent";
 
    //Get related posts with new API, if none, it'll return 0, if any, the ID (hence not 0).
    $related_post = toolset_get_related_post( $post_id_current, $relationship_to_query, $role);
 
    return  $related_post;
}
add_shortcode( 'be-get-parent-id', 'be_get_parent_id' );
#901293

Not entirely sure what you want to achieve, however this is Custom code that is not anymore related to Toolset.

If you want to pass the single variables in ShortCode attributes you can do so according the DOC about it:
https://codex.wordpress.org/Shortcode_API

For example I can pass the relation type and the parent slug in the shortcode attributes:

function be_get_parent_id($atts){
   

    global $post;
    $post_id_current = $post->ID;
   
    $a = shortcode_atts( array(
        'relationship' => '',
        'role' => '',
    ), $atts );
 

  

    $relationship_to_query = $a['relationship'];
    $role = $a['role'];
  
    //Get related posts with new API, if none, it'll return 0, if any, the ID (hence not 0).
    $related_post = toolset_get_related_post( $post_id_current, $relationship_to_query, $role);
  
    return  $related_post;
}
add_shortcode( 'be-get-parent-id', 'be_get_parent_id' );

That I use as:

[be-get-parent-id role="child" relationship="toolset-post-type-toolset-parent"]

It will output the single post related in that relationship in child role.
0 if none.

#901294

changing status

#901295

Okay, thanks again for the help. All good to go here.

- Aaron