Skip Navigation

[Gelöst] View query filter multiple conditions for the same field

This support ticket is created vor 4 Jahren, 7 Monaten. 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

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

Zuletzt aktualisiert von Jim vor 4 Jahren, 7 Monaten.

Assistiert von: Waqar.

Author
Artikel
#1579937

Jim

Hello Toolset Fam!

Hope everybody is well.
Another day, another question...

I have created a field (radio options) named 'specials' for my custom post type:

Specials:
- empty value (default)
- sA
- sB
- sC

Now I want to filter out posts that are 'sB' or 'sC', so only show posts with 'specials' not set (empty) or as 'sA'

I have setup my query filter like this:
versteckter Link

This kinda works...
If I set my post to sA it shows and when set to sB or sC it does not show.

I found out it's because those posts have this value not set.

Is there a way to make this work, or somehow set the default value for all the posts?

Or maybe some other solution..?

#1580447

Jim

I noticed my question was not very clear...
The title does not reflect the issue anymore. I started with that question but found out about it while writing this support ticket...

The thing now is this:
I created that field "specials" on a website that has many posts already, so this field is not set for all the old posts. It will only be set once I open the post and edit it.

So I thought to check in my query filter (see the cloudapp link) if the post is NOT sB or sC. That way if the post does not have that value set it will be true.

But unfortunately that does not work.

So do you have a solution to this?
Hope that's more clear...

#1581057

Hi Jim,

Thank you for contacting us and I'd be happy to assist.

For what you're planning to achieve, it is important that at least 1 custom field value for the custom field "specials" is available for all the new and existing posts in that post type.

I'll recommend these steps:

1. First, please update the "specials" field's settings, so that even the default/empty first option has some value, for example, '0'.

Example screenshot: versteckter Link

As a result, whenever you'll add a new post and no radio option is selected for the "specials" field, a custom field record with '0' will be added.

2. For the already added posts, you'll need to programmatically check for all the posts without any record for this custom field and set it.

Here is a code snippet for a shortcode that can work:


add_shortcode('update_empty_custom_fields', 'update_empty_custom_fields_func');
function update_empty_custom_fields_func($atts) {

	$a = shortcode_atts( array(
		'type' => '',
		'field' => '',
		'value' => ''
	), $atts );

	// get all published posts from a specific post type
	$args = array(
				'post_type'        => $a['type'],
				'posts_per_page'   => -1,
				'post_status'      => 'publish'
			);
	$posts_array = get_posts( $args );

	// loop through those posts 1 by 1
	foreach ($posts_array as $post) {
		// get current value of the field
		$current_value = get_post_meta( $post->ID, 'wpcf-'.$a['field'], true );

		// if it is empty set the provided default value
		if( empty($current_value) || !isset($current_value) ) {
			update_post_meta( $post->ID, 'wpcf-'.$a['field'], $a['value'] );
		}
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

To use it, you can place this shortcode in the content of any temporary page and view that page on the front-end:

Example:


[update_empty_custom_fields type="book" field="specials" value="0"]

Please replace "book" and "specials" with the actual slug of your custom post type and the custom field "specials", respectively.

Note: Once the custom field values have been updated, you can delete this temporary page and remove that custom code snippet from the website.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1581127

Jim

Hi Waqar!

Thanks very much, this is going to work because then all posts will have a value set.

But...
I ran the code but nothing happened unfortunately.

My shortcode:
[update_empty_custom_fields type="journal-article" field="special-article" value="a0"]

function in my functions file:

add_shortcode('update_empty_custom_fields', 'update_empty_custom_fields_func');
function update_empty_custom_fields_func($atts) {
 
    $a = shortcode_atts( array(
        'type' => '',
        'field' => '',
        'value' => ''
    ), $atts );
 
    // get all published posts from a specific post type
    $args = array(
                'post_type'        => $a['type'],
                'posts_per_page'   => -1,
                'post_status'      => 'publish'
            );
    $posts_array = get_posts( $args );
 
    // loop through those posts 1 by 1
    foreach ($posts_array as $post) {
        // get current value of the field
        $current_value = get_post_meta( $post->ID, 'wpcf-'.$a['field'], true );
 
        // if it is empty set the provided default value
        if( empty($current_value) || !isset($current_value) ) {
            update_post_meta( $post->ID, 'wpcf-'.$a['field'], $a['value'] );
        }
    }
}

Do you see anything wrong?

#1582041

Hi Jim,

Thanks for writing back and your steps seem to be correct.

Can you please share temporary admin login details along with the link to the pages with the post view and this shortcode?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1584717

Jim

Hello Waqar,

I found the problem!
It wasn't your code, I had something else wrong and therefor thought the code didn't work but it does!

Problem solved 🙂
Thanks very much.

Now I ran into anew problem but I will open a new ticket for it because it's something completely different.
Have a great day.

#1584721

Jim

My issue is resolved now. Thank you!