Skip Navigation

[Resolved] Query a view using a date field from a related post

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

Problem: I have a one-to-many post relationship set up between Trial Days and Showings. I would like to display a list of Showings from the Trials Days with a custom field date in the last few days.

Solution: You can set up a View of Trial Days, filtered by the trial date custom field. Apply your 'n' number of days to the query filter of this View. You can place this View on your site temporarily to confirm the correct Trial Days are being displayed.

Next create a View of Showings, filtered by post ID, set by a shortcode attribute "ids". In the Loop of this View, add the post title for now. You can modify the display later. Next, add this code in your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom code:

function ts_get_recent_children_func( $atts ){
  $parent_view_id = 12345;
  $child_view_id = 56789;
  $relationship_slug = 'some-slug';
 
  // do not edit below this line
 
  $parents = get_view_query_results( $parent_view_id );
  $query_parents = array(
    'parent' => array_column($parents, 'ID'),
  );
  $child_ids = toolset_get_related_posts(
    $query_parents,
    $relationship_slug,
    array(
      'parent',
      1000000,
      0,
      array(),
      'post_id',
      'child'
    )
  );
  $child_view_args = array(
    'id' => $child_view_id,
    'ids' => implode(',', $child_ids)
  );
  $children = render_view( $child_view_args );
 
  return $children;
}
add_shortcode( 'ts-get-recent-children', 'ts_get_recent_children_func' );

You must edit the parent View ID to match the numeric ID of your View of Trial Days. You must edit the child View ID to match the numeric ID of your View of Showings. You must also edit the relationship slug to match your Trial Days > Showings relationship slug. You can find that in Toolset > Relationships when you edit this relationship. Finally, insert this custom shortcode where you want to display recent Showings:

[ts-get-recent-children]

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

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

Last updated by sarahK-2 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1178144

I have a query listing information from post type "showings". This is a slider view. I want it to display (randomly if possible, but if not that's okay) "showings" from the last 'n' days.

Each "trail day" has many "showings". (trial days:showings is one:many)
"trail day date" is a custom field of custom post type "trial day".
"showings" is the custom post type I am displaying in the view.

"trial day date" is the field I would like to filter by.

I would like to list only recent "showings" (last 90 days, for example). I am having trouble using the Query Filter while building the view, I'm assuming because the field I am trying to filter by is from a related post type(trial day), and not directly within the post type(showing) I am displaying. I am able to display "trial day" field "trial day date" within the view, I'm just not able to filter the entire view with this date.

Thanks for any help you can give me.

#1178547

HI, right now it's not possible to set up a filter in wp-admin based on custom fields of related posts. This feature is planned in an upcoming release, but I don't have an ETA available right now. Instead, you must use custom code to filter the posts programmatically. You can set up a View of Trial Days, filtered by the trial date custom field. Apply your 'n' number of days to the query filter of this View. You can place this View on your site temporarily to confirm the correct Trial Days are being displayed. Then use the get_view_query_results API to fetch those parent posts programmatically, and use the toolset_get_related_posts API to fetch all the child Showings of those parent Trial Day posts. Pass those child Showing IDs into the post__in query attribute of a View of Showings, and your results will display Showings from the past 'n' number of days. I can help with this code if you need assistance.

https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#1178918

Christian, thank you. I have set up a Trial Days view that only lists days within the date range, and verified that it is displaying only those dates.

After that point, yes I do need help with the code 😉 The references you sent me to are a bit above my level.

If you could write for me basically what I should put (and where I should copy and paste that code into within the Showing slider view) I would appreciate that. Let me know if I need to provide you with additional information (or access) for you to help me with this. I also have someone I can ask for help with this if I absolutely need it, so let me know if you think that is more appropriate.

Also, I know you said that there is no ETA for the feature to be released, but if you think it will be within the next two months, I am happy to wait for it, too.

All the best,
Sarah

#1179253

First, you must create a View of Showings, filtered by post ID, set by a shortcode attribute "ids". In the Loop of this View, add the post title for now. You can modify the display later. Next, add this code in your child theme's functions.php file, or create a new snippet in Toolset > Settings > Custom code:

function ts_get_recent_children_func( $atts ){
  $parent_view_id = 12345;
  $child_view_id = 56789;
  $relationship_slug = 'some-slug';

  // do not edit below this line

  $parents = get_view_query_results( $parent_view_id );
  $query_parents = array(
    'parent' => array_column($parents, 'ID'),
  );
  $child_ids = toolset_get_related_posts(
    $query_parents,
    $relationship_slug,
    array(
      'parent',
      1000000,
      0,
      array(),
      'post_id',
      'child'
    )
  );
  $child_view_args = array(
    'id' => $child_view_id,
    'ids' => implode(',', $child_ids)
  );
  $children = render_view( $child_view_args );

  return $children;
}
add_shortcode( 'ts-get-recent-children', 'ts_get_recent_children_func' );

You must edit the parent View ID to match the numeric ID of your View of Trial Days. You must edit the child View ID to match the numeric ID of your View of Showings. You must also edit the relationship slug to match your Trial Days > Showings relationship slug. You can find that in Toolset > Relationships when you edit this relationship. Finally, insert this custom shortcode where you want to display recent Showings:

[ts-get-recent-children]

Let me know if you run into issues.

#1179317

Thank you Christian, that worked perfectly.