[Gelöst] Conditional output using post availability
This support ticket is created vor 2 Jahre, 9 Monate. 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.
I have a site using Toolset that enables users to create a subcription. Each time a subcription is made, a post "adhesion" is created. In this post, there is a custom fields that indicates the year of the subscription (annee-universitaire).
Now, I would like to create a conditional output based on these 2 elements :
1. The fact that for the loogedin user, an "adherent" post has been created (if no post, false)
2. The post includes a "annee-universitaire" field that is equal to a specific shortcode : [annee_univers]
Is it possible and what would be the format of this condition?
Regards
Pat
Hello Pat and thank you for contacting Toolset support.
Well for the 1st condition, you'll need to use the get_posts function. Keep in mind that, by default, the function will return published and private posts. Check all the possible arguments that can be passed to the function here https://developer.wordpress.org/reference/classes/wp_query/parse_query/
This would be the simplest case:
For the 2nd condition, I am not sure to understand. Will the shortcode [annee_univers] returns the value of the custom field? Or should the custom field be equal to the return value of the shortcode?
Thanks for taking care.
Concerning point 2, I think this could be achieved by taking into account that the annee-universitaire is also a field of a specific post_id (12345). So the code could be :
Now, to explain the situtation, I was managing access to specific part of the site thanks to conditional output. This conditional output was based on the role of the users :
[wpv-conditional if="('[wpv-current-user info="logged_in"]' eq 'true') AND (('[wpv-current-user info="role"]' ne 'subscriber') AND ('[wpv-current-user info="role"]' ne 'admincu') AND ('[wpv-current-user info="role"]' ne 'administrator'))"]
How can I move from this conditional output based on role management to conditional output based on "adherent" post validation?
Well, adding a custom field into the get_posts arguments, or more generally, the WP_Query argument needs to be done through the "meta_query" key. Something like:
Then, to be able to use this custom validation inside conditionals, you will need to add it, either on a custom shortcode or a custom function. Then you will need to register that shortcode or that function in Toolset->Settings->Front-end Content, so it can be used inside conditionals. Then you can use it inside a conditional shortcode. The user interface will help you generate the condition for the shortcode. https://toolset.com/course-lesson/using-toolset-conditional-block/#checking-for-shortcodes-values
Just tested it and found an issue.
I have created the funtion (remplacement-role) and registered it.
I have added this code in a post :
[wpv-conditional if="( remplacement-role() eq '0' )"]
Pas d'adhésion
[/wpv-conditional]
[wpv-conditional if="( remplacement-role() ne '0' )"]
Avec adhésion
[/wpv-conditional]
The result is that nothing is displayed in the frontend !
I was thinking that the condition was based on the number of posts found, so placing 0 in the conditional output should give me the right condition?
There is also an error reported in the Toolsed parameters for the function.
The error is :
2021-06-29 15:46:10, frontend] A problem occurred when executing snippet "remplacement-role". The result of include_once is: ""
Just to explian the last code : the post ID 18207 is used to store some global variables for the site and one of them is the university year 'annee-en-cours'
Then, when a user pays a yearly subscription, a postype 'adhesion' is created and the year of the subcription is placed in the 'annee-universitaire' field.
It is not possible to have a middle score in a function name. WordPress shortcode may have a middle score, but PHP functions cannot.
Check this article hidden link
Use an underscore instead, "remplacement_role" instead of "remplacement-role". On the other hand, the code of the function seems correct to me.
Please note that this code will work as expected if the "annee-universitaire" meta data stores the year. If it is a date field, it would not work, because Toolset date fields are stored as timestamps. hidden link
And if it is a Toolset defined field, it will need to have the "wpcf-" prefix:
Another point : I have checked in the database to be sure of the exact keys to use. I have replaced 'annee-en-cours' by 'wpcf-annee-en-cours'
I have also checked that all keys exist in the different related posts and have the same value (2021).
But this does not change anything and I'm still getting 0 as the shortcode result !
Regards
Pat
Sorry for my last post but it seems some cache effects have not updated the toolset php file (in Toolset customization), so the modifications was not taken into acount.
I have refreshed all pages and now, everything is working fine.
Thanks for your help.
Regards
Pat
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.