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:
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.
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...
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 versteckter 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
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.
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.
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
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.
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
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.
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:
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
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.
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.