Skip Navigation

[Resolved] Conditional output using post availability

This support ticket is created 3 years, 5 months ago. 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.

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: Africa/Casablanca (GMT+01:00)

This topic contains 9 replies, has 2 voices.

Last updated by Pat 3 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#2099171

Pat

Hello,

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

#2100497

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:

$posts = get_posts( array(
    'post_type' => 'adherent',
    'author' => wp_get_current_user()->ID,
) );
return count( $posts );

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?

In both cases, you might need to register the shortcode or the custom function in Toolset->Settings to be able to use them inside conditionals. Check these articles:
- https://toolset.com/course-lesson/using-toolset-conditional-block/#checking-for-shortcodes-values
- https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/

#2101053

Pat

Hi Jamal,

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 :

$post_id = 12345;
$annee = get_post_meta( $post_id,  'annee-universitaire', true );
$posts = get_posts( array(
    'post_type' => 'adherent',
    'author' => wp_get_current_user()->ID,
   'annee-universitaire'=>$annee,
) );
return count( $posts );

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?

Regards
Pat

#2101195

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:

$post_id = 12345;
$annee = get_post_meta( $post_id,  'annee-universitaire', true );
$posts = get_posts( array(
    'post_type' => 'adherent',
    'author' => wp_get_current_user()->ID,
   'meta_query' => array(
        array(
            'key'     => 'annee-universitaire',
            'value'   => $annee,
            'compare' => '=',
        ),
    ),
) );
return count( $posts );

Read more about the WP_Query arguments in this article https://developer.wordpress.org/reference/classes/wp_query/

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

#2102217

Pat

Hi Jamal,

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?

Regards
Pat

#2102227

Pat

Jamal,

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: ""

And my final code is :

$post_id = 18207;
$annee = get_post_meta( $post_id,  'annee-en-cours', true );
$posts = get_posts( array(
    'post_type' => 'adhesion',
    'author' => wp_get_current_user()->ID,
   'meta_query' => array(
        array(
            'key'     => 'annee-universitaire',
            'value'   => $annee,
            'compare' => '=',
        ),
    ),
) );
return count( $posts );

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.

Hope this helps.
Regards
Pat

#2102271

Hello Pat,

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:

            'key'     => 'wpcf-annee-universitaire',
#2103135

Pat

Hi Jamal,

Things seems to move forward (no more error), but now, the result of the shortcode is always 0 even if I create a post.
Here is my last code :

function remplacement_role () {
$post_id = 18207;
$annee = get_post_meta( $post_id,  'annee-en-cours', true );
$posts = get_posts( array(
    'post_type' => 'adhesion',
    'author' => wp_get_current_user()->ID,
   'meta_query' => array(
        array(
            'key'     => 'wpcf-annee-universitaire',
            'value'   => $annee,
            'compare' => '=',
        ),
    ),
) );
return count( $posts );
}
// You can remove the following shortcode definition once you complete the code
add_shortcode('role-replacement', 'remplacement_role');

For info, if I replace return count( $posts ); by return "Hello"; I'm still getting 0 with the shortcode !!!

Any idea?
Regards
Pat

#2103153

Pat

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

#2103763

Pat

Hi Jamal,

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