Skip Navigation

[Resolved] Get the list if parent id which does not have any child.

This support ticket is created 3 years, 4 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
- 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 1 reply, has 2 voices.

Last updated by Luo Yang 3 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#2233773

Hello,

I like to list all the id that does not have child. Basically what i want is one filter that differentiate the post which contains the child and others which does not.

I have created the FAQ post type and ANSWERS which connected with one ( FAQ ) to many ( ANSWERS ) now in admin area it's difficult to check if FAQ has answers or not. so I am creating the filter for that not issue it how and what value should pass in in query so it will only show FAQ which answered or which unanswered like this - hidden link

Here is the code.

if ( is_admin() ) {
// this hook will create a new filter on the admin area for the specified post type
add_action( 'restrict_manage_posts', function(){
global $wpdb, $table_prefix;

$post_type = ( isset( $_GET['post_type'] ) ) ? sanitize_text_field( $_GET['post_type'] ) : 'post';
$faq_type = ( isset( $_GET['faq_type'] ) ) ? sanitize_text_field( $_GET['faq_type'] ) : '';

// only add filter to post type you want
if ( $post_type == 'faq' ) {
?>
<select name="faq_type">
<option value="" <?php selected( $faq_type, '' ); ?>>All FAQs</option>
<option value="answered" <?php selected( $faq_type, 'answered' ); ?>>Answered</option>
<option value="unanswered" <?php selected( $faq_type, 'unanswered' ); ?>>Unanswered</option>
</select>
<?php
}
});

function emma_faq_admin_filter( $query ) {
if ( is_admin() && 'faq' === $query->get('post_type') && $query->is_main_query() ) {
$faq_type = ( isset( $_GET['faq_type'] ) ) ? sanitize_text_field( $_GET['faq_type'] ) : '';
if ( $faq_type ) {
if ( 'answered' === $faq_type ) {
$query->set( 'post__in', array( 180557 ) ); // list of faq ids
} elseif ( 'unanswered' === $faq_type ) {
$query->set( 'post__in', array( 180905 ) ); // list of faq ids
} else {
}
}
}
}
add_action( 'pre_get_posts', 'emma_faq_admin_filter', 500000000 );
}

Any suggestions how to achieve this.

Thank you.

#2233809

Hello,

There isn't such kind of built-in feature within Toolset plugins, it needs custom codes, for example:

In your custom PHP codes, you can get all FAQ post ids(for example $faq_ids) with WP function get_posts():
https://developer.wordpress.org/reference/functions/get_posts/
Us each FAQ post id, you can get the related child Answer posts with function toolset_get_related_posts():
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
If there isn't any related child Answer posts, then remove this FAQ post id from $faq_ids

Then use the result in your PHP codes, like this:
$query->set( 'post__in', $faq_ids );

See similar thread here:
https://toolset.com/forums/topic/parametric-search-filtering-by-parent-taxonomy-custom-date-field/#post-1732731