Skip Navigation

[Closed] Filter post in form relationship

This support ticket is created 2 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.

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

Last updated by Nigel 2 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#2268347

Tell us what you are trying to do?

I have a relationship from two posts (A and B). I want to filter post A when connecting to post B, based on a field value of post A.

#2268497

Nigel
Supporter

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

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

You are connecting A posts to a B post, and want to modify the list of available A posts according to a field of post type A.

That's not possible using the UI, and there are no API functions to facilitate this either.

We might be able to find a way to intervene in the list of A posts offered, but before going any further, are you connecting the posts on the back end edit page or in a relationship form on the front end?

#2268525

I'm connecting the posts on the front end relationship form.

I wait some updates from you.

THX

#2269255

Nigel
Supporter

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

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

I checked the source code and we don't have any hooks to intervene in the process, but I did identify a way in to manipulate the query generated by the ajax request to populate the select dropdown with possible posts to connect.

I should say this is a relatively advanced topic, if you are not comfortable with PHP you will need to find a developer that can help. I'll share some sample code below, but you will need to adapt it for your particular case, and I can't do that for you.

The example code is as follows:

add_action( 'wp_ajax_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
// if guest users can use your relationship form then uncomment the following line
// add_action( 'wp_ajax_nopriv_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
function ts_mod_suggested_posts(){

	add_action( 'pre_get_posts', 'ts_pre_get_posts' );
	function ts_pre_get_posts( $query ){
	
		if ( $query->query['post_type'] == 'left' )
		{
			// you can set additional query parameters for post type 'left' here

		} elseif ( $query->query['post_type'] == 'right' )
		{
			// you can set additional query parameters for post type 'right' here
			$query->set( 'meta_key', 'wpcf-priority' );
			$query->set( 'meta_value', '1' );
			$query->set( 'compare', '=' );
		}
	}
}

In this example, the relationship form connects 'left' and 'right' post types, and the query to retrieve posts of the 'right' type is modified to include a condition that the posts should have a value for the custom field 'priority' of 1. (Toolset custom fields are stored in wp_postmeta with a prefix of 'wpcf-'.)

I hope that helps.

#2269489

Hello,
i modify your code like that:

add_action( 'wp_ajax_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
// if guest users can use your relationship form then uncomment the following line
// add_action( 'wp_ajax_nopriv_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
function ts_mod_suggested_posts(){

add_action( 'pre_get_posts', 'ts_pre_get_posts' );
function ts_pre_get_posts( $query ){

if ( $query->query['post_type'] == 'milite' )
{
// you can set additional query parameters for post type 'right' here
$query->set( 'meta_key', 'wpcf-stato-servizio' );
$query->set( 'meta_value', '1' );
$query->set( 'compare', '=' );
}
}
}

I want that everytime i make a relationship with the post_type "milite", the proposed post are filtered by wpcf-stato-servizio = 1.

This code is not working. I'm making some mistake?

#2270177

Nigel
Supporter

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

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

That looks correct, assuming that the post type you are searching is 'milite' and that the custom field details are correct.

If you share access to your site I can take a look.

Let me mark your next reply as private so that we can get log-in credentials from you—you may want to create a temporary admin user for us to use that you can later delete. And be sure to have a current backup of your site.

Can you confirm where I can find the form to connect the posts?

The topic ‘[Closed] Filter post in form relationship’ is closed to new replies.