Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+01: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?
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+01: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-'.)
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?
Les langues: Anglais (English )Espagnol (Español )
Fuseau horaire: Europe/London (GMT+01: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.