Skip Navigation

[Gelöst] Query Filter in Views for Blank field

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to create a custom shortcode that finds all the "person" posts where the author is the current logged-in User, and filter those posts by two custom fields. One of the fields must equal "Yes" and the other field is either blank or does not exist.

Solution: Use an associative array meta query to query by two custom fields:

/* Check if cash gift has any messages */
function cash_gift_message_func( $atts ){
   global $current_user;
   wp_get_current_user();
   $all_posts = get_posts(
    array(
      'numberposts'   => -1,
      'author' => $current_user->ID,
      'post_type'     => 'person',
      'meta_query' => array(
        'relation' => 'AND',
        'message_clause' => array(
          'relation' => 'OR',
          array(
            'key' => 'wpcf-message-for-recipient',
            'compare' => '!=',
            'value' => ''
          ),
          array(
            'key' => 'wpcf-message-for-recipient',
            'compare' => 'NOT EXISTS'
          )
        ),
        'gift_clause' => array(
            'key' => 'wpcf-cash-gift',
            'compare' => '=',
            'value' => 'Yes'
        )
      )
    )
  );
     
       $exec_number = count($all_posts);
return $exec_number;
}
     
add_shortcode( 'cash_message_func', 'cash_gift_message_func' );

Relevant Documentation:
https://developer.wordpress.org/reference/classes/wp_meta_query/

This support ticket is created vor 4 Jahren, 11 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
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)

Dieses Thema enthält 30 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von geoffD vor 4 Jahren, 10 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1398579

Hello

I have created a view with a loop to display all CPT 'People'. I need to add a query filter that only brings back 'People' where the 'message-for-recipient' field has been filled in and is not blank.. I can't seem to find a way to do this..

e.g. Message For Recipient is a string !blank or !empty

Many thanks

Best regards

Geoff

#1398843

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

What you can do is to sort the view by this field. If the Field isn't filled out then it won't appear in the view .

Thanks,
Shane

#1399023

Hi Shane
The problem is that I have a <h2> Title that displays between the item found and loop begin section... so this still displays even though the field is empty...
Regards
Geoff

#1399811

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

Would you mind sending me a link to the page for this issue ?

Thanks,
Shane

#1399813

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

Alternatively you can try this doing this in your view.

[wpv-conditional if="( NOT(empty($(wpcf-message-for-recipient))) )"]

This field is not empty

[/wpv-conditional]

Please try this in your view and let me know if this helps.
Thanks,
Shane

#1399881

HI Shane

The problem is that the <h2></h2> and <p></p>tags I need to hide/display (depending on whether posts are found is outside of the loop so the conditional code you suggest wouldn't work. Here is the loop below. I only need to show the <h2></h2> and <p></p> if the items found have the field 'message-for-recipient' not empty..so the filter needs to be in the search rather than the view....e.g.

'Select items with field: message-for-recipient is not empty'

Hope that makes sense

Best wishes

Geoff

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
<h2>Messages</h2>
<p>It is my intention that my Trustees deliver the following messages to my intended beneficiaries, either electronically or in hard copy. These personal messages should not be shared with anyone other than the beneficiaries listed, and as a non testamentary document these messages should be excluded from public availability.</p>


	<ol class="wpv-loop js-wpv-loop">
		<wpv-loop>
         
			[wpv-conditional if="( $(wpcf-message-for-recipient) ne '' )"]<li>[wpv-post-body view_template="loop-item-in-messages-to-my-possession-recipients" suppress_filters = "true"]</li>[/wpv-conditional]
       
		</wpv-loop>
	</ol>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"][/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

#1399887

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

Would you mind providing a link to this view so that I can have a look to try something ? I might be able to get this to work for you.

Thanks,
Shane

#1399963

Hi Shane

Here is the link:

versteckter Link

In the layout editor it is the very last row (46)

You should be able to use the login details from the other ticket that is still open..

Best regards

Geoff

#1400801

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

I've added a sorting option to the view.

This will ensure that the items without a recipient won't show up in the view.

Could you check the view and let me know if it is ok now ?

Thanks,
Shane

#1400859
Screen Shot 2019-12-05 at 15.00.25.png

Hi Shane

I think you may have misunderstood...To demonstrate I have created a single 'cash gift' which is actually a CPT 'PERSON'... but have left the message-for-recipient field blank..

So in the view in question it still finds this one entry and although doesn't output anything for the field it still displays the <h2> and <p> tags after the items-found tag... I've attached a screenshot to show you this:

Best regards

Geoff

#1401625

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

I had checked this for you but it seems because the other ticket for the permissions was resolved the credentials were removed.

I see on your People CPT that you have 2 persons added. Could you let me know which one should be displayed?

Also please add the credentials in this thread once more.

Thanks,
Shane

#1401807

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

I've modified the query filter.

Please check now to see if this helps.

Thanks,
Shane

#1402797

Hi Shane

I think you still don't understand..I will try to explain more clearly...

The view returns CPT 'PERSON' that have either 'Cash Gift' OR 'Possession' eq to 'Yes'
The CPT may or may not have the 'message-for-recipient' field filled in.
In the view itself if the filter finds any items it will display :
[wpv-items-found]
<h2>Messages</h2>
<p>It is my intention that my Trustees deliver the following messages to my intended beneficiaries, either electronically or in hard copy. These personal messages should not be shared with anyone other than the beneficiaries listed, and as a non testamentary document these messages should be excluded from public availability.</p>

And also Loop Items...
[/wpv-items-found]

However sometimes the filter will find CPT 'PEOPLE' that do not have the 'message-for-recipient' field filled in. If this is the case I need the :
<h2>Messages</h2>
<p>It is my intention that my Trustees deliver the following messages to my intended beneficiaries, either electronically or in hard copy. These personal messages should not be shared with anyone other than the beneficiaries listed, and as a non testamentary document these messages should be excluded from public availability.</p>
To be HIDDEN... or for the filter to only find posts where the 'message-for-recipient' field is filled in...

So either:
1. The filter to only find posts where the 'message-for-recipient' field is filled in...
2. Hide the <h2> & <p> part on the items found section if ALL of the returned posts do not have the 'message-for-recipient' field filled in...

Hope this makes more sense now..

Thanks as always for your support and help.

Best regards
Geoff

#1403455

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Geoff,

Correct I believe I understand. In this case you will only want your view to display PEOPLE that only have the message for recipients filled in.

Can you create just 2 people for my account to test ? One with the message field filled out and one without ?

We should be able to achieve this by using a query filter as you've done but i'm not sure why its not working as intended so I need to test it further with these 2 options.

Thank you for your continued patience as we work through this one.
Thanks,
Shane

#1403457

Hi Shane

Thanks for your continued support..

Correct I believe I understand. In this case you will only want your view to display PEOPLE that only have the message for recipients filled in. ------No this is where there is a misunderstanding - The filter already does this... It is the :

<h2>Messages</h2>
<p>It is my intention that my Trustees deliver the following messages to my intended beneficiaries, either electronically or in hard copy. These personal messages should not be shared with anyone other than the beneficiaries listed, and as a non testamentary document these messages should be excluded from public availability.</p>

...part that I need hiding if the found items ALL have no message for recipients.. SO I was either suggesting :

1. The filter to only find posts where the 'message-for-recipient' field is filled in...
2. Hide the <h2> & <p> part on the items found section if ALL of the returned posts do not have the 'message-for-recipient' field filled in...

Thanks again

Best regards

Geoff