Skip Navigation

[Resolved] Filter children out if parent status is not publish

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a View that shows child posts based on several filter criteria. I would also like to add a filter that only shows child posts whose parent post is published.

Solution: Use wpv_filter_query to check each parent post's status, and build a list of post IDs to define the query's 'post__in' array.

add_filter('wpv_filter_query', 'filter_children_by_parent_markt_published_func', 101, 3);
function filter_children_by_parent_markt_published_func($query, $view_settings, $view_id) {
  if ( $view_id == 377) {
    global $wpdb;
    $ids = array();
    $metas = $wpdb->get_results( "SELECT meta_value,post_id FROM wp_postmeta WHERE meta_key = '_wpcf_belongs_markt_id'" );
    foreach($metas as $id) {
      if(isset($id->meta_value) && get_post_status($id->meta_value) == 'publish') {
        $ids[] = $id->post_id;
      }
    }
    $query['post__in'] = isset($query['post__in']) ? array_intersect($query['post__in'],$ids) : $ids;
  }
  return $query;
}

Relevant Documentation: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-filter-query

This support ticket is created 7 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Marco Bolleter 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#557258

I created a view which shows child posts (Angebote) based on different filter criterias. As additional filter criteria a only want to list child posts (Angebote) if the parent post (Markt) is in status "publish".

Based on input from https://toolset.com/forums/topic/filter-children-of-draft-parent/ I added the following coding to the function.php file ...

add_filter( 'wpv_filter_query', 'only_markt_publish', 198, 2 );
function only_markt_publish( $query_args, $settings ) {
if($settings['view_id'] = 377){
if(get_post_status(get_the_ID()) == 'publish'){
return $query_args;
}else{
return null;
}
}
}

... but the child posts (Angebote) of parents in status "draft" are still listed in the view.

#557324

Hi, this code was set up to be used on a View placed on a single parent post, so it won't work when you're looping over all children of all parents. Please try the following code instead:

add_filter('wpv_filter_query', 'filter_children_by_parent_markt_published_func', 101, 3);
function filter_children_by_parent_markt_published_func($query, $view_settings, $view_id) {
  if ( $view_id == 377) {
    global $wpdb;
    $ids = array();
    $metas = $wpdb->get_results( "SELECT meta_value,post_id FROM wp_postmeta WHERE meta_key = '_wpcf_belongs_markt_id'" );
    foreach($metas as $id) {
      if(isset($id->meta_value) && get_post_status($id->meta_value) == 'publish') {
        $ids[] = $id->post_id;
      }
    }
    $query['post__in'] = isset($query['post__in']) ? array_intersect($query['post__in'],$ids) : $ids;
  }
  return $query;
}

You may have to modify this line if your markt post type slug is not "markt":

$metas = $wpdb->get_results( "SELECT meta_value,post_id FROM wp_postmeta WHERE meta_key = '_wpcf_belongs_markt_id'" );

Replace the 'markt' in '_wpcf_belongs_markt_id' with the slug of your markt post type.

#557367

Hi Christian
It works now - great! I had to change the Select to the Table: "981d6_postmeta".

$metas = $wpdb->get_results( "SELECT meta_value,post_id FROM 981d6_postmeta WHERE meta_key = '_wpcf_belongs_markt_id'" );