Skip Navigation

[Gelöst] Filter by custom field and repeatable fields

This support ticket is created vor 2 Monaten, 3 Wochen. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Nigel vor 2 Monaten, 3 Wochen.

Assistiert von: Nigel.

Author
Artikel
#2743655
Screenshot_4.png

Tell us what you are trying to do?
I am trying to create a filter that has custom fields and repeatable custom fields (for example 'wpcf-rc-camps-pow') of the 'rc-camps' group for posts of the custom type 'rc-roll-call'.
Now I have created a filter displayed on page as 2 groups of filters, the first group of parameters by custom fields, the second group by repeatable fields. The results are displayed correctly if I use parameters from the first group or from the second group as I added the condition of displaying fields depending on the post type [ wpv-conditional if="( '[wpv-post-type]' eq 'rc-roll-call' )"] or [wpv-conditional if="( '[wpv-post-type]' eq 'rc-camps ' )"].

The problem is that when I want to use parameters from the first and second groups, it does not work.

What should I do so that the filters from the first and second groups can work simultaneously and find posts with the post type 'rc-roll-call' without setting the display condition in the search results?
Is there any documentation that you are following?
to generate field output depending on post type used this
https://toolset.com/documentation/legacy-features/views-plugin/conditional-html-output-in-views/
Is there a similar example that we can see?
- I didn't find any
What is the link to your site?
versteckter Link

#2744228

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi there

I think the problem is that you are querying two different post types (the main post type and the child post type which stores the repeatable groups of fields) and trying to filter by fields that belong to one or the other but not both.

By default filters would be evaluated with AND, and any one post (of either type) cannot meet field conditions of fields that belong to it as well as fields that do not belong to it.

So the solution would be to switch the evaluation to OR. You may be able to do that within the UI in the Query Filter section of the View (if you cannot see it, expose it via the Screen Options tab at the top of the page).

Otherwise you would need to change that using the wpv_filter_query API hook (see https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query).

Here's an example of code that can do that which you could try (you'll need to edit the View ID):

/**
 * Compare custom field filters with OR instead of AND
 */
  function tssupp_filter_query( $view_args, $view_settings, $view_id )  {
       
     if ( in_array( $view_id, array( 123 ) ) and isset( $view_args['meta_query'] ) ) { // Edit View ID

        $view_args['meta_query']['relation'] = 'OR';
    }
    
    return $view_args;
  }
  add_filter( 'wpv_filter_query', 'tssupp_filter_query', 101, 3 );
#2744506

Hi Nigel, thank you for reply

But cross search doesn't anyway after the wpv_filter_query changed relationship to "OR". Is there any way to make a search to work when parameters from post custom field and repeatable fields are chosen at the same time?

also currently Query Filter UI doesn't allow to changes AND operator to OR, so had to add code

#2744827

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

I just set up a test of this on my local site, and it works as intended when using the custom code.

Can I check your site to see that you have it set up correctly?

Let me mark your next reply as private so that we can get log-in credentials from you—you may want to create a temporary admin user for us to use that you can later delete. And be sure to have a current backup of your site.

Can you give me clear steps to test, including some filter values I should select that you would expect to produce results.

#2745566

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

OK, what you are describing isn't possible, unfortunately.

It is logically not possible to query multiple post types with filters for fields that are unique to the different post types and combine the filters with AND rather than OR.

In your case, you don't actually want to query the RFG post type, you want to query roll call posts, but be able to filter those posts both by fields which belong to them and by fields which do not (because they belong to the RFG posts).

Because of how RFGs are implemented in Toolset, via child posts, you need to filter posts by fields of related posts (the child posts), and that is not supported I'm afraid. You can only filter posts by fields that belong to those posts, which means the fields you want to filter by that are part of repeatable field groups really need to be reassigned so that they belong directly to the posts and are not part of repeatable groups.

I don't really have a solution for you. If you wanted to try and get it to work it would involve hooking into the WP_Query filters responsible for generating the JOIN and WHERE clauses of the resulting SQL query and modifying them, but it is a complex and ambitious task.

I'm sorry I can't help any further.