Home › Toolset Professional Support › [Resolved] Search post without relation
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
Tagged: Post-relationship
This topic contains 8 replies, has 2 voices.
Last updated by Minesh 5 years, 7 months ago.
Assisted by: Minesh.
Hi,
I would like to search all posts without relationship.
I have two post types connected together. And i'd like to see / search which post was created without child and also search which post doesn't have parent.
Thanks a lot.
Hello. Thank you for contacting the Toolset support.
Well - as I understand, do you mean that you want to display the parent post list which do not have any child posts and child post list which do not have any parent assigned?
Yes Exactly 🙂
Well - you should create the view to display your parent posts and add the following code to your current theme's functions.php file:
function func_check_post_has_child( $query, $view_settings, $view_id ){ $relationship = 'student-student-reivew'; // Edit for relationship slug if ( in_array( $view_id, array( 99999) ) && !empty( $query->posts ) ){ foreach ($query->posts as $key => $parent) { // get the child posts of the parent post $children = toolset_get_related_posts( $parent->ID, $relationship, array( 'query_by_role' => 'parent', 'role_to_return' => 'child' ) ); $number_of_children = count( $children ); if ( $number_of_children !== 0 ) { }else{ unset( $query->posts[$key] ); $query->found_posts = $query->found_posts - 1; $query->post_count = $query->post_count - 1; } } $query->posts = array_values( $query->posts ); } return $query; } add_filter( 'wpv_filter_query_post_process', 'func_check_post_has_child', 1001, 3 );
Where:
- Replace 'student-student-reivew' with your original relationship slug
- replace 99999 with your view ID'
It will only display the parent posts that has a child.
Hi, i think you made a mistake because i told you :
I want to display the parent post list which do not have any child posts and child post list which do not have any parent
And in your view it is wrote : func_check_post_has_child
I tried to update but it bit complicated.
To display the child posts which do not have a parent, we need another view that should display the child posts because with one views its not logically possible.
- You should create another view that should display the child posts and add the followng hook:
add_filter( 'wpv_filter_query_post_process', 'func_check_post_has_parent', 1001, 3 ); function func_check_post_has_parent( $query, $view_settings, $view_id ){ $relationship = 'student-student-reivew'; // Edit for relationship slug if ( in_array( $view_id, array( 222 ) ) ){ foreach ($query->posts as $key => $child) { // get the child posts of the parent post $children = toolset_get_related_post( $child->ID, $relationship ); if ( $children !== 0 ) { unset( $query->posts[$key] ); $query->found_posts = $query->found_posts - 1; $query->post_count = $query->post_count - 1; } } $query->posts = array_values( $query->posts ); } return $query; }
Where:
- Replace 'student-student-reivew' with your original relationship slug
- replace 99999 with your view ID'
I think you made a mistake again.
I need two things.
- See parents that doesn't have chiild
- See childs that doens't have parent
You create first fonction for my first needs : func_check_post_has_child
But that does't work.
I create a page : hidden link
I create view : hidden link
And my code below.
And the view sho all parents with child and not parent without child.
I don't understand the code, so i can say if you have made mistake or if it doesn't work.
The strange thing is the name of your function : func_check_post_has_child
I will name it func_check_post_has_not_child
Thanks a lot for your help.
----
//on récupère les programmes sans lot
function func_check_post_has_child( $query, $view_settings, $view_id ){
$relationship = 'programme-lot'; // Edit for relationship slug
if ( in_array( $view_id, array( 24385) ) && !empty( $query->posts ) ){
foreach ($query->posts as $key => $parent) {
// get the child posts of the parent post
$children = toolset_get_related_posts(
$parent->ID,
$relationship,
array(
'query_by_role' => 'parent',
'role_to_return' => 'child'
)
);
$number_of_children = count( $children );
if ( $number_of_children !== 0 ) {
}else{
unset( $query->posts[$key] );
$query->found_posts = $query->found_posts - 1;
$query->post_count = $query->post_count - 1;
}
}
$query->posts = array_values( $query->posts );
}
return $query;
}
add_filter( 'wpv_filter_query_post_process', 'func_check_post_has_child', 1001, 3 );
I need two things.
- See parents that doesn't have chiild
- See childs that doens't have parent
==> Yes, I understand your requirement.
Well - I need access details to check why the code I shared is not working on your install.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Well - I just made a test and added the following test post which do not have any child:
=> hidden link
And I can see its working - its only displaying post that do not have any child:
=> hidden link
Can you please confirm.
The code is adjusted as given under:
//on récupère les programmes sans lot function func_check_post_has_child( $query, $view_settings, $view_id ){ $relationship = 'programme-lot'; // Edit for relationship slug if ( in_array( $view_id, array( 24385) ) && !empty( $query->posts ) ){ foreach ($query->posts as $key => $parent) { // get the child posts of the parent post $children = toolset_get_related_posts( $parent->ID, $relationship, 'parent', 100, 0, array(), 'post_id', 'child' ); if ( !empty($children) ) { unset( $query->posts[$key] ); $query->found_posts = $query->found_posts - 1; $query->post_count = $query->post_count - 1; } } $query->posts = array_values( $query->posts ); } return $query; } add_filter( 'wpv_filter_query_post_process', 'func_check_post_has_child', 1001, 3 );
Now, I see you added a view inside view. so where you want to display the view that do not have parent?