Hi Minesh,
Thanks the filtering option in the screenshot works fine.
But I do have another form for admin but in this I want to show all the public post and in private I want to show only the current user post. I alter the code you provided but its not working .
add_action( 'wp_ajax_toolset_select2_suggest_posts_by_post_type', 'ts_mod_suggested_posts', 1 );
// Uncomment below if guest users can use the relationship form
// 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 ( ! is_admin() && $query->is_main_query() ) {
$post_type = $query->get( 'post_type' );
if ( $post_type === 'group' ) {
$current_user_id = get_current_user_id();
// Allow public posts
$tax_query = [
[
'taxonomy' => 'group-status',
'field' => 'slug',
'terms' => ['public'],
]
];
if ( $current_user_id ) {
// Also allow private posts by current user
$tax_query[] = [
'taxonomy' => 'group-status',
'field' => 'slug',
'terms' => ['private'],
];
// Only include private posts by the current user
$meta_query = [
'relation' => 'OR',
[
'key' => 'group-status',
'compare' => 'NOT EXISTS',
],
[
'key' => 'group-status',
'value' => 'private',
'compare' => '=',
]
];
$query->set( 'author', $current_user_id );
}
$query->set( 'tax_query', $tax_query );
$query->set( 'post_status', 'publish' );
}
}
}