I am trying to: Filter results based on 2 filters. The first being filter by custom field value is equal to or greater than a constant. The second is by a custom field by a related post
Link to a page where the issue can be seen:
hidden link
This page should only be showing results where the 'Trust Share Current Period' is equal to or greater than 15
I have managed to get the greater than filter working - i was using string not number
I am having difficulty with another filter though. I have 2 custom post types, Trusts & Reps. I am querying the Trusts post type for posts which have a value greater than 15 AND the related Rep Post has a custom field value 'HIVN09'.
With my current dataset, this should return 1 result
I am having difficulty with another filter though. I have 2 custom post types, Trusts & Reps. I am querying the Trusts post type for posts which have a value greater than 15 AND the related Rep Post has a custom field value 'HIVN09'.
Hi, this type of multi-source, multi-field filtering isn't currently possible in simple Views. Right now, it's only possible to filter by the fields of the posts included in the Content Selection editor. We plan to include some more robust filtering for related posts in an upcoming release, but it's not ready yet. One way you can achieve similar results in the current system is to nest two filtered Views.
- Create a View of Reps, filtered by the Reps custom field value, and also filtered by a post relationship (choose your Trust - Reps relationship), set by the current post in the loop. This will effectively show matching Reps for each matching Trust.
- Skip the Loop Wizard.
- In the Loop Editor, just before the wpv-loop tags, insert a Post Title shortcode. Use the "Post Selection" tab in the popup to select a related post, and choose the Trust post from your Trust-Reps relationship. The system will insert the correct code to display the Trust post title.
- Now insert this View of Reps in the Loop of your View of Trusts. Remove the Trust post information from the loop of the View of Trusts.
So now you have a View of Trusts that is used to loop over all the Trust posts matching the Trust custom field filter, and then in that loop you're looping over all related Reps posts matching the Repst custom field filter. The Trust post title is only displayed if there are matching Reps posts. If not, the Trust title is not displayed.
The other option is a custom code solution. We have the following APIs available for PHP query manipulation and post relationship information:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
I'm available to help if you get stuck using those APIs.
Hi Christian,
Thank you for the really helpful advice.
I was wondering if you could give me a hand with the APIs?
I am using a standard WP_Query to get each of the posts in my Post Type 'rep'. Within this loop I'd like to get each of the related posts that belongs to the post in 'rep'. I have tried using the toolset_get_related_posts() but I'm not sure I am understanding it correctly.
Would you mind providing a bit of guidance on this?
Many Thanks
Ben
Within this loop I'd like to get each of the related posts that belongs to the post in 'rep'. I have tried using the toolset_get_related_posts() but I'm not sure I am understanding it correctly.
Yes of course, what is the slug of the post relationship, and what are the slugs of each post type? Which post type is parent and which is child?
Thank you!
Parent: rep
Child: trust
Relationship Slug: reps-trusts
Knowing the rep ID, you can use the following code to get an array of child trust IDs:
$rep_id = 12345; // your rep post ID
$relationship_slug = "reps-trusts"; // the relationship slug
$trust_ids = toolset_get_related_posts(
$rep_id,
$relationship_slug,
array(
'parent', // this is the role of the known ID (the rep)
1000000, // a positive integer is required for the limit
0, // pagination
array(), // other query parameters, you can leave this empty
'post_id', // whether you want an array of post IDs or post objects
'child' // the role of the unknown (trust)
)
);
Hi!
Thank you for sending that over. I seem to be getting some errors from this, unfortunately, please see below:
Fatal error: Uncaught InvalidArgumentException: The role name to query by is not valid. Allowed values are: "parent", "child", "intermediary". in /var/www/vhosts/immaculate.co.uk/g1830.immaculate.co.uk/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php:206 Stack trace: #0 /var/www/vhosts/immaculate.co.uk/g1830.immaculate.co.uk/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php(158): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->set_query_by_elements(12, NULL) #1 /var/www/vhosts/immaculate.co.uk/g1830.immaculate.co.uk/wp-content/plugins/types/vendor/toolset/toolset-common/inc/public_api/m2m.php(103): OTGS\Toolset\Common\Interop\Commands\RelatedPosts->__construct(12, 'reps-trusts', Array) #2 /var/www/vhosts/immaculate.co.uk/g1830.immaculate.co.uk/wp-content/themes/twentynineteen-child/page-display.php(74): toolset_get_related_posts(12, 'reps-trusts', Array) #3 /var/www/vhosts/immaculate.co.uk/g1830.immaculate.co. in /var/www/vhosts/immaculate.co.uk/g1830.immaculate.co.uk/wp-content/plugins/types/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/related_posts.php on line 206
You can view the entire code at this link: hidden link
Let's try an alternate syntax where the parent part of an array:
$rep_id = $repPostID; // your rep post ID
$relationship_slug = "reps-trusts"; // the relationship slug
$trust_ids = toolset_get_related_posts(
array('parent' => array($rep_id)),
$relationship_slug,
array(
'parent', // this is the role of the known ID (the rep)
1000000, // a positive integer is required for the limit
0, // pagination
array(), // other query parameters, you can leave this empty
'post_id', // whether you want an array of post IDs or post objects
'child' // the role of the unknown (trust)
)
);
That worked perfectly, thank you Christian!
Great, let me know if you run into additional problems.