Skip Navigation

[Resolved] Connecting Related Posts

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

Problem: I would like to use Toolset's one-to-many post relationships to link posts of the same post type.

Solution: Toolset's relationship system doesn't support relationships among posts of the same post type. You can work around this by using a custom taxonomy. Create terms in the taxonomy that correspond to each post's slug. Then assign the related terms' slugs to each related post. This can be automated with some custom code.

This support ticket is created 5 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 22 replies, has 2 voices.

Last updated by martinE-4 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1268811

So the generic field option values are post slugs, correct?
My form is:

[credform]
	<div class="form-group">
	<label>Choose a related resource</label>
	[cred_generic_field type='select' field='related-resource' class="" urlparam=""]
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[ [wpv-view  name="all-resources"] ]
}
[/cred_generic_field]
      
      [cred_field field='related' force_type='taxonomy' output='bootstrap' class='form-control']
[cred_field field="related_popular" taxonomy="related" type="show_popular"]
	</div>

	[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

so the options are my view "all-resources" which is just the JSON for the titles of the posts (not the slugs). Here is my view for the generic select ('related-resource'):

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-item index=1]{"value":"[wpv-post-id]","label":"[wpv-post-title]"}[wpv-item index=other],{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
[wpv-layout-end]

All I see in the generic select dropdown are the post titles but I assume the slug and the post-id is available also?

To get the value of the selected item in the callback, look for the generic field slug as a key in the $_POST superglobal. If the generic field slug is "my-generic-select" then you will find the selected value in $_POST['my-generic-select'];

Since my generic field slug is 'related-resource' I should use

$tag = $_POST['related-resource'] 

as i have already in my function to find the slug of the selected related resource post.

To convert that selected post slug to a post ID you can use the WordPress API get_page_by_path

should I use?...

//Returns the selected resource post id '$post_id_selected' with the slug $tag'
get_page_by_path('$tag', $post_id_selected, 'resource');

To get the post slug of the current post, use the WordPress API get_post().

$post_slug = get_post_field( 'post_name', get_post() );

So my final CRED action on submit should be:

/**
 * add function for CRED form edit Resource with Related Resource
 */
   
function set_term_from_generic_select( $post_id, $form_data ) {
  $forms = array( 10191 );
// first assign the selected slug to the current post 'related' taxonomy
  if ( in_array( $form_data['id'], $forms ) ) {
    $taxonomy = 'related';
    $tag = $_POST['related-resource'];
    wp_set_object_terms( $post_id, $tag, $taxonomy, true );
// then assign the current post slug to the selected post 'related' taxonomy to be bi-directional
    get_page_by_path('$tag', $post_id_selected, 'resource');
   $post_slug = get_post_field( 'post_name', get_post() );
    wp_set_object_terms( $post_id_selected, $post_slug, $taxonomy, true );
  }
}
add_action('cred_save_data', 'set_term_from_generic_select',100,2);

How am I doing so far?

#1270035

All I see in the generic select dropdown are the post titles but I assume the slug and the post-id is available also?
If you check the data produced by your View, you can see each option has only two attributes: value and label. The label is used to define what you see displayed in the select field options. The value is the post's ID, and this is the data that will be passed back to your cred_save_data callback. When someone selects an option in the select field, the value corresponding to that title will be be sent back in the $_POST superglobal.

get_page_by_path('$tag', $post_id_selected, 'resource');

Since the post ID is already in the $_POST object, get_page_by_path isn't necessary. I misunderstood and thought you would have the post slug in the value instead of the post ID. You should delete this line.

$post_slug = get_post_field( 'post_name', get_post() );

Okay you are using get_post_field, that's great. No need to get_post() in this case, you can just use the available $post_id parameter instead since it is the new post's ID:

$post_slug = get_post_field( 'post_name', $post_id );
wp_set_object_terms( $post_id_selected, $post_slug, $taxonomy, true );

The variable $post_id_selected isn't defined. I think you could use $tag instead, since this holds the ID of the selected parent post:

wp_set_object_terms( $tag, $post_slug, $taxonomy, true );
#1270095

Almost there...

function set_term_from_generic_select( $post_id, $form_data ) {
$forms = array( 10191 );
  if ( in_array( $form_data['id'], $forms )) {
// part 1 - assign generic field selected post id to current post taxonomy
       $taxonomy = 'related';
       $tag = $_POST['related-resource'];
	wp_set_object_terms( $post_id, $tag, $taxonomy, true );
// part 2 - assign current post id to selected post taxonomy
	$post_slug = get_post_field( 'post_name', $post_id );
        wp_set_object_terms( $tag, $post_slug, $taxonomy, true );

  }
}
add_action('cred_save_data', 'set_term_from_generic_select',100,2);

But this assigns the current post slug to the generic field selected post. Actually I'm now using the post id in my taxonomy since that is a nice short number and it works well in the view filter on my single resource content template where I filter the posts with IDs set by the view shortcode attribute wpvrelated="[wpv-post-taxonomy type="related" format="name"]"

So I need to make a slight change to the last 2 lines of the function to use wp_set_object_terms to set the current post id into the generic selected post taxonomy? (not the post slug which is currently being set)

#1270099

Okay so you can delete the $post_slug line if you don't need the post slug for anything. Then in the next line use $post_id instead of $post_slug, since you want to assign the current post ID in the generic post..

#1270107

Yes I tried this but it's not setting the generic post taxonomy at all to anything?

function set_term_from_generic_select( $post_id, $form_data ) {
$forms = array( 10191 );
  if ( in_array( $form_data['id'], $forms )) {
    $taxonomy = 'related';
    $tag = $_POST['related-resource'];
	 wp_set_object_terms( $post_id, $tag, $taxonomy, true );
    wp_set_object_terms( $tag, $post_id, $taxonomy, true );

  }
}
add_action('cred_save_data', 'set_term_from_generic_select',100,2);

Although experimentally (just trying to understand what's going on) I tried this line before the 2nd wp_set_object_terms

   $post_slug = get_post_field( 'post_name', $post_id );
   wp_set_object_terms( $tag, $post_slug, $taxonomy, true );

and that assigned the slug of the current post to the generic post (so close).

Something wrong with $post_id?

#1270109

Not sure, but it could have to do with the fact that your term slugs are numeric:
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

Integers are interpreted as term IDs, not term slugs. That could explain why nothing gets saved since there is no term with this numeric ID. Try converting the post ID to a string so it's not recognized as an integer ID:

wp_set_object_terms( $tag, strval($post_id), $taxonomy, true );
#1270185

Yes, using strval solved the remaining issue. My function works great now! Thank you for all your help!

#1271299
Datastan_Meets_Storyland__Surfing_the_Zeitgeist_Without_Wiping_Out_–_ICASC_MIgration_1.jpg

Sorry to have another question about my form. (It's coming along quite nicely thanks to you!)

I'm trying to add the feature of selectively removing related resources (the taxonomy values) using the CRED taxonomy input field. Of course it's not quite what I want because it offers input when actually I only want to allow the user to optionally delete taxonomy values. (see uploaded image)

Any ideas?