Hi Nigel,
To refresh your memory, my site a relationship between a section custom post type (which is an instance of a course) and a catalog custom post type. There is also a custom taxonomy representing the department. So, the section for Drawing Fundamentals lives in the "Drawing" category and is associated with the Spring 2019 catalog.
I want to generate a list of all section posts associated with the Spring 2019 catalog in the Drawing category. Here's what I've got so far:
$posts_in_category = get_posts (
array (
'post_type' => 'section',
'numberposts' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'catalog_category',
'field' => 'id',
'terms' => $term_info->term_id, // The ID of a given category
'include_children' => false
)
)
)
) ;
This generates a list of all section posts in the Drawing category, regardless of catalog. How do I limit the section posts to a specific catalog?
Saul
Hi Saul,
Thank you for contacting us and I'll be happy to assist.
To create a list of section posts, based on the connected taxonomy terms and the post-relationship, you can use Toolset view and use its "Query Filter" settings.
https://toolset.com/documentation/getting-started-with-toolset/create-and-display-custom-lists-of-content/
Please check this screenshot from an example view that I've set up:
Screenshot: hidden link
You'll see that the view is set to get a list of "Section" posts, but with 2 "Query Filters":
- The first one is set to limit the list to only a particular taxonomy term "Departments 1"
- The second one is set to limit the list to only those sections, which are related to particular catalog "Catalog 3".
Note: If you need the output of your view inside the PHP code, you can use "render_view" function from the "Views API Functions":
https://toolset.com/documentation/programmer-reference/views-api/
I hope this helps.
regards,
Waqar
Waqar,
Thanks for your note. These instructions are for creating a View using the plugin's graphical interface. I need to filter the list of posts using PHP's get_posts() function. Do you have any pointers regarding how I need to modify the code snippet below? My guess is I need to add a 'post__in' key, but I don't know if there's an easy way to specify the ID of the parent custom post. Is there a syntax to point to wp_toolset_associations somehow?
$posts_in_category = get_posts (
array (
'post_type' => 'section',
'numberposts' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'catalog_category',
'field' => 'id',
'terms' => $category_id, // Current category ID
'include_children' => false
)
)
)
) ;
Thank you.
Saul
Hi Saul,
If you'd prefer to get the results, without using Toolset's view, you can split the code into two parts:
1. First one will get all section posts related to a taxonomy term, using "get_posts".
( https://codex.wordpress.org/Template_Tags/get_posts )
2. The second part will loop through all returned section posts and check if a relationship exists between each post with your specified catalog post, using the "toolset_get_related_posts" function.
( https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )
Following two example code snippets will make this more clear:
https://toolset.com/forums/topic/parent-cant-be-identified-without-grandparent/#post-1200437
https://toolset.com/forums/topic/list-authors-content-only-in-the-selection-field/#post-1197063
regards,
Waqar
I'll give it a try. Thanks, Waqar!
Saul