Skip Navigation

[Resolved] Display posts with a relationship

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

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 11 replies, has 2 voices.

Last updated by Luo Yang 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1155958

Tell us what you are trying to do?
I am attempting in PHP to display a table of posts that all have a certain relationship. I have recently upgraded to the new relationship style.

Is there any documentation that you are following?
I have been reading https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Is there a similar example that we can see?

$semen = get_posts( array(
'numberposts' => -1,
'post_type' => 'semen',
'meta_query' => array(
array(
'key' => '_wpcf_belongs_agreement_id',
'compare' => 'EXISTS',
)
)
));

#1156070

Hello,

The codes you mentioned above is for legacy parent/child post type relationship, since you have already migrated your website to the new post type relationships, so you can use the new API function toolset_get_related_posts(), see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

And there is example codes in above document, you can click "Usage examples" link and see it.

#1156793

OK so i read that already.
i thought the "related to" argument was mandatory?
How would i return all posts that have a parent CPT of a certain type? like my example above.
I want to return all posts

$semen 

that have a parent of

_wpcf_belongs_agreement_id

Tim

#1157505

Thanks for the detail, you are right, the "related to" argument is mandatory.

See source codes of Types plugin, file \types\vendor\toolset\toolset-common\inc\autoloaded\wp_query_adjustments\abstract.php, line 26:

* - 'related_to': int|WP_Post - post to which the results are connected. Mandatory.

And it supports only one single parent post's ID/post object.

So in your case, I suggest you create a post view:
- query posts of child post type "semen"
- in the View's loop, use [wpv-conditional] to check if it's parent "agreement" post's ID, then display "semen" information, for example:

[wpv-conditional if="( '[wpv-post-id item="@agreement-semen.parent"]' ne '' )"] here display semen information [wpv-post-link] [/wpv-conditional]

Please replace "agreement-semen" with the one-to-many relationship slug between post types "agreement" and "semen"

#1157525

not working for me 🙁

		
<wpv-loop>
[wpv-conditional if="( '[wpv-post-id item="@agreement_semen.parent"]' > 0 )"] 
			<tr>
				[wpv-post-body view_template="Loop item in Semen with Agreements"]
			</tr>
 [/wpv-conditional]
		</wpv-loop>

It appears to timeout.

Tim

#1157526

It works fine in my localhost, please check these:
1) you are using the latest version of Toolset plugins
2) In case it is a compatibility problem, please deactivate other plugins, and switch to wordpress default theme 2017, and test again
3) If the problem still persists, please provide your website credentials, also point out the problem page URL and view URL, I need to test and debug it in a live website, thanks

#1157561

Thanks for the details, I can see the problem in your website, since there are 17,697 "Semen" posts in your website, it conducts the "timeout" problem you mentioned above.

In your case, it does need a filter in the post view:
return all 'semen' CPT that have a parent 'agreement' CPT

But as you can see, there isn't such a built-in feature within current Views plugin + new post type relationship, if you agree, we can take it as a feature request, our developers will evaluate it.

#1157574

any php suggestions? As you can see from my original code what i was able to achieve pre-upgrade.

Tim

#1157612

No, there isn't existed custom PHP codes workaround for it, since the new post type relationships are not using custom fields to store the value, so your custom PHP codes won't work as expected after upgrade.

But I will check it with our developers for custom codes possibility, will update here if there is anything news.

#1158618

It is possible wit some PHP codes, I have setup a demo in your website:
1) Dashboard-> Toolset-> Settings-> Custom Code,
hidden link

code "display-posts-with-relationship", add below PHP codes:

<?php
/**
 * New custom code snippet.
 */
toolset_snippet_security_check() or die( 'Direct access is not allowed' );

function no_parent_query_func($query_args, $view_settings, $view_id){
	if($view_id == 344294){
		add_filter( 'posts_where', 'no_parent_posts_where_func', 10, 2 );
	}
	return $query_args;
}

function no_parent_posts_where_func($where){
	global $wpdb;
	$relationship_slug = 'agreement_semen';
	$where .= ' AND ' . $wpdb->posts . '.ID NOT IN( 
				SELECT child_id FROM ' . $wpdb->prefix . 'toolset_associations t1 
				LEFT JOIN '. $wpdb->prefix . 'toolset_relationships t2 ON t2.id = t1.relationship_id 
				WHERE t2.slug = \'' . $relationship_slug . '\' )';
	
	return $where;
}

function remove_no_parent_posts_where_func($query_args, $view_settings, $view_id){
	if($view_id == 344294){
		remove_filter( 'posts_where', 'no_parent_posts_where_func');
	}
	return $query_args;
}

add_filter( 'wpv_filter_query', 'no_parent_query_func', 99, 3 );
add_filter( 'wpv_filter_query_post_process', 'remove_no_parent_posts_where_func', 10, 3 );

Notice:
344294 is the view's ID of below step 2)
'agreement_semen' is the specific post type relationship slug

2) Modify the post view "Semen with Agreements",
hidden link
- enable option "Pagination enabled with manual transition and page reload"
- in section "Pagination and Sliders Settings", add some Pagination shortcode:

Total [wpv-found-count] items
<ul class="pagination">
	<li class="page-item">[wpv-pager-prev-page][wpml-string context="wpv-views"]Previous[/wpml-string][/wpv-pager-prev-page]</li>
	<li class="page-item">[wpv-pager-next-page][wpml-string context="wpv-views"]Next[/wpml-string][/wpv-pager-next-page]</li>
</ul>

- in section "Loop Editor", remove the [wpv-conditional] shortcode, since we have already applied the filter using PHP codes.

Test it in front-end:
hidden link

Please check if it is what you need

#1160177

if the relationship has been migrated can i now access like

toolset_get_related_post( $semenid, 'bull-record_semen' );

and setup relationships like

toolset_connect_posts( 'bull-record_semen-batch-test', $batch_test['bullid'], $newtest->ID );
#1160188

Yes, you are right, if the relationship has been migrated, then you can get related posts information using function toolset_get_related_post() and setup setup relationships with function toolset_connect_posts(), see our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Feel free to create new ticket if there is any new problem, thanks