Skip Navigation

[Resolved] Filter isn't working as expected

This support ticket is created 5 years, 11 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 10 replies, has 2 voices.

Last updated by Christian Cox 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1181550
Screenshot 2019-01-10 at 15.45.56.png

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

#1181610

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

#1181639

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.

#1184298

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

#1184383

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?

#1184389

Thank you!

Parent: rep
Child: trust
Relationship Slug: reps-trusts

#1184465

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)
    )
  );
#1184518

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

#1184520

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)
		    )
		  );
#1184522

That worked perfectly, thank you Christian!

#1184525

Great, let me know if you run into additional problems.