Skip Navigation

[Résolu] Query Filter in Views for Blank field

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 Il y a 4 années et 4 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Marqué : 

This topic contains 30 réponses, has 3 voix.

Last updated by geoffD Il y a 4 années et 4 mois.

Assisted by: Christian Cox.

Auteur
Publications
#1403459

Sorry Shane I forgot to say that I have set up 2 PEOPLE, both have no message for recipient ..so in this example I don't want 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>

showing....If either or both PEOPLE had a message for recipient then I would want the above message to show...

Hope that makes more sense now

Best regards
Geoff

#1404917

Shane
Supporter

Languages: Anglais (English )

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

Hi Geoff,

Take a look at this below.
hidden link

I've setup 2 test Persons, one with a message and one without. The Person without a message doesn't show up.

If you were to remove the Test 1 post you will notice that nothing shows in the view, this is because the message section was not filled out.

Please have a look with the account that you created for me and let me know if this is ok now.

Thanks,
Shane

#1404985
Screen Shot 2019-12-10 at 21.53.28.png

Hi Shane

I have removed the 'message-for-recipient' content from both your test PERSON's but still 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>

is showing on the hidden link ......page...

This is what I need to hide if no messages are present..and it is in the [items found] section of the view..I have attached an image also

Many thanks

Geoff

#1405909

Shane
Supporter

Languages: Anglais (English )

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

Hi Geoff,

The way the view is setup currently now, it will not list out any items that don't have the message field filled out.

Perhaps what i need clarity on is if the view should still list out these items?

Currently i've set it so that only posts where the message field is filled out will display, this will in turn cause your message to display. If there are no posts with messages then the message in the view won't display at all.

This is why I setup the Post Test 1 and Test 2 where one has the message field filled out and the other doesnt. If I was to delete the message from the posts then the view will not show anything at all.

Hopefully we are on the same page now.

Thanks,
Shane

#1405933

Shane
Supporter

Languages: Anglais (English )

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

I can write a custom query for this, however it has to be an AND query where the post will only be displayed if the gift option is yes, the possession is yes and the message field is empty.

#1406149

Hi Shane

The output of the view is not the issue, the issue is that I have some text <h1><p> that sits in the item found section. This text only displays when PEOPLE are found...the problem is that when all the people that have been found don't have a message I don't want the <h1><p> text to be shown

SO an example:
1. PEOPLE FOUND IN LOOP and at least one of them has as message to recipient
<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>

MESSAGE(S) TO RECIPIENT HERE

2. PEOPLE FOUND IN LOOP but none of them has as message to recipient

***THIS TEXT HIDDEN
<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>
***THIS TEXT HIDDEN

NO MESSAGES

I hope this makes more sense..I'm maybe describing the problem badly...

Best wishes and thanks as always for your help and support

Geoff

#1406193

Shane
Supporter

Languages: Anglais (English )

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

Hi Geoff,

I'm clear on what you want to achieve.

Thats not what i'm asking. In order for this section not to display the view must not be returning any results at all.

What I am asking is If you want the view to only display the loop content IF and only IF these 3 conditionals are met.
1. The message field is empty.
2. The gift is set to YES
3. The possession is set to YES.

The problem is that the comparison logic must be an AND comparison which means all 3 criterias must be met. This will generate a view that will display Persons only when all 3 conditions are met.

Otherwise from this it is not possible for us to hide the message
<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>

Hopefully this clarifies what i'm asking as this is the only way to achieve your goal.

Thanks,
Shane

#1406241

Hi Shane

A PERSON can either be a cash gift OR a possession but not both so the 3 rules you give wouldn’t work

1. The message field is empty.
2. The gift is set to YES
3. The possession is set to YES.

Is there a way of using a hook and short code so a conditional tag could be used on the text
E.g.
Return all Persons with either possession or cash gift = Yes
Go through each one and check if message for recipient is blank
If all posts are blank return 1
Short code =1

Then in loop
Condition - if shortcode =1
Don’t display text

Let me know what you think
Best regards
Geoff

#1407129

Shane
Supporter

Languages: Anglais (English )

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

Hi Geoff,

Ok So this is actually not possible.

Given the fact that where your message is being placed and the Criteria in which you don't want the message to be displayed. Its not possible to perform such a logic in views even if we use a custom filter to do it.

I'm not sure how best we can resolve this one. Perhaps you can make the gift option and possession options taxonomies but this would mean you will need to change the logic of all your other systems that you've setup to now work with the taxonomy option.

In this case we can perform an OR and AND logic. The main problem is that we can't combine the OR an AND while using just custom fields.

Thanks,
Shane

#1411999

Hi Shane

I've written this function but I'm not sure how to pass 2 meta keys and values... This function should:

1. Find all CPT 'PERSON' where wpcf-cash-gift = "Yes' AND wpcf-message-for-recipient = BLANK

It then should add up all of the posts found and return this number...I can then use the shortcode in the view

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_key_1'   => 'wpcf-cash-gift',
            'meta_value_1' => 'Yes',
'meta_key_2'   => 'wpcf-message-for-recipient',
            'meta_value_2' => ''
			)
        );
   
       $exec_number = count($all_posts);
return $exec_number;
}
   
add_shortcode( 'cash_message_func', 'cash_gift_message_func' );
#1413607

Hello, Shane is out for the holidays so I'll try to help. To query on two custom fields, you can use a meta_query associative array something like this:

$all_posts = get_posts(
  array(
    'numberposts'   => -1,
    'author' => $current_user->ID,
    'post_type'     => 'person',
    'meta_query' => array(
        'relation' => 'AND',
        'gift_clause' => array(
            'key' => 'wpcf-cash-gift',
            'compare' => '=',
            'value' => 'Yes'
        ),
        'message_clause' => array(
            'key' => 'wpcf-message-for-recipient',
            'compare' => 'NOT EXISTS'
        )
    )
  )
);

Here's the documentation for more advanced meta queries:
https://developer.wordpress.org/reference/classes/wp_meta_query/

Let me know if I have misunderstood the request and I can give you some additional feedback.

#1413691

Hi Christian

Thanks so much for your help and for picking this ticket up...It seems to be taking so long to get to a solution... The code that you have amended looks like it should work however the shortcode is returning '0' when it should be returning the number count.

So for example I have
3 CTP PERSON
All 3 have wpcf-cash-gift = 'Yes'
But only 1 has a 'wpcf-message-for-recipient'
So the shortcode [cash_message_func] should return '2'
But on each occasion it returns only 0

Here is the full function:

/* 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',
        'gift_clause' => array(
            'key' => 'wpcf-cash-gift',
            'compare' => '=',
            'value' => 'Yes'
        ),
        'message_clause' => array(
            'key' => 'wpcf-message-for-recipient',
            'compare' => 'NOT EXISTS'
        )
    )
  )
);
    
       $exec_number = count($all_posts);
return $exec_number;
}
    
add_shortcode( 'cash_message_func', 'cash_gift_message_func' );
/* END - Check if cash gift has any messages */

I shall look forward to hearing from you
Best regards
Geoff

#1414867

Okay where can I see your test on the site? I don't see any published person posts, so as far as I can tell "0" would be the correct response right now.

#1414991

Hi Christian

Ok I have set up 3 x CPT 'PERSON' on your login - each PERSON has cash-gift ='Yes'
But only 1 has a massage-for-recipient

SO the [cash_message_func] should currently equal 2

The Cash Gifts are set up and editable here:
hidden link

I've added the shortcode to test at the bottom of this page:
hidden link

Many thanks for your help

Regards
Geoff

#1415361

Ok I have set up 3 x CPT 'PERSON' on your login - each PERSON has cash-gift ='Yes'...But only 1 has a massage-for-recipient...SO the [cash_message_func] should currently equal 2
I'm confused on the number that should be returned by cash_message_func. The instructions above say it should return '2', but the instructions below imply it should return '1' (Number of Persons where I am the author, with both Cash Gift = "Yes" and message for recipient = "blank"):

1. Find all CPT 'PERSON' where wpcf-cash-gift = "Yes' AND wpcf-message-for-recipient = BLANK
It then should add up all of the posts found and return this number.

Is one of these a typo, or am I misunderstanding the calculation? For now, the shortcode returns 1. I made an adjustment to handle both a "blank" message field and a non-existent message field, since either is technically possible and both should be counted here.

/* 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' );
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.