Skip Navigation

[Resolved] Show all others but those who has certain value in radio field

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

Problem:
How to only include posts in the results of a View where a custom field equals some value, but where the custom field might not exist for some posts.

Solution:
If you use the != comparison with a custom field filter it will exclude any posts where the field is not set, in which case you would need to modify the View arguments using the API to add a test for whether the field exists. (That's how WordPress queries work.)

An example of such code is given below: https://toolset.com/forums/topic/show-all-others-but-those-who-has-certain-value-in-radio-field/#post-1259327

The client decided to use a taxonomy instead which doesn't have this same problem.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 4 years, 10 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
- 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+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by katjaL 4 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1259015

I have custom woocommerce product archive with custom dropdown filters and there are over 600 products.

Now we got this new custom radio field ("custom product" with values "yes" and "no") and we want to show in basic archive only those products which have not selected "yes". This way we wouldn't have to go through all the old products and select "no", but only add the "yes" to the few custom products (which we don't want to show in the basic archives).

My problem now is that I don't get the filter work as expected. I have tried it as a boolean (no is 0 and value different from 0) or a string and all other combinations I can think of, but with no success. All products stop showing with my new filter.

Would you please point me to the right direction if I am approaching this from wrong angle, thank you.

#1259327

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

The way WordPress queries work is that if you add a condition such as some-custom-field != 0 then the results will exclude all posts with some-custom-field = 0 and also all posts where nothing is saved for that custom field (there is no matching key in wp_postmeta).

For such a query to work you also need to include posts where the custom field key doesn't exist (so that you can imply "include" for posts where it is not set).

To do that you will need to use the wpv_filter_query API hook (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query) and modify the meta_query so that where it includes a test for this radio field such as:

            array(
                    'key' => 'wpcf-custom-product',
                    'value' => 0,
                    'type' => 'NUMERIC',
                    'compare' => '!='
            )

you need to convert that to also test whether the key exists, e.g.

array(
            array(
                    'key' => 'wpcf-custom-product',
                    'value' => 0,
                    'type' => 'NUMERIC',
                    'compare' => '!='
            ),
            array(
                'key' => 'wpcf-custom-product',
                'compare' => 'NOT EXISTS'
            ),
            'relation' => 'OR'
        )

I don't know what your coding is like, is that something you are able to work on implementing?

#1265619

Thank you Nigel, this was very informative. For my problem I actually found another way, I used a new taxonomy for it. I didn't realize before there was a difference of how to include or exclude with taxonomy/custom field. Now I know with taxonomy's help you can exclude, with custom field it's not that easy.

So everything ok here, thanks!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.