Skip Navigation

[Resuelto] Conditional output

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I need to use this conditional output : define if the current user is author of a "subscription" post and if yes, if the subscription post date is between the "variable" custom fields start-date and end-date.

Solution:

The client fix it with custom shortcode, see his post:
https://toolset.com/forums/topic/conditional-output-8/#post-704283

Relevant Documentation:

This support ticket is created hace 6 años, 7 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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: Asia/Hong_Kong (GMT+08:00)

Etiquetado: 

Este tema contiene 8 respuestas, tiene 3 mensajes.

Última actualización por Pat hace 6 años, 7 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#673232

Pat

Hello,

I'm working on a new site and needs to use a conditional output based on this :

- I'm using a postype to define yearly variable => "variable". For that, I have created a "variable" post (let say ID=30) with some specific custom fields (start-date, end-date). This post will be updated each new year.
- Then, I have another postype to create subcriptions ("subscription"). That means, when user wants to subscribe, they validate a Cred form that is linked to the specific product. A new post "subcription" is created and assigned to the user as author with the postdate = creation date.
- Now, I need to use this conditional output : define if the current user is author of a "subscription" post and if yes, if the subscription post date is between the "variable" custom fields start-date and end-date.

Hope I'm sufficiently clear;
Regards
Pat

#674041

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - to check the current user is loggedin user - you can use [wpv-conditional] shortcode.

For example:

[wpv-conditional if="( '[wpv-current-user info="login"]' eq '[wpv-post-author format="meta" meta="user_login"]' ) "]
current user is same as the post author of current post.
[/wpv-conditional]

But what is your another condition to be check?

More info:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-user
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#674372

Pat

Hi Minesh,

Thanks for taking care.
I'm not willing to detect if the user is connected or not (this is already done in another part of the page).

I want to detect if the connected user is the author of a post (postype : subcription) and if this post date is between two dates that are defined by the custom fields (start-date and end-date) of the post (postype : variable) and with id=30

So, conditional should be :
post (subcription) with author=connected user exists ? (there could be several post with the same author, but only one with the further date condition)
AND
post date (subscription) is between post (variable) start-date and post (variable) end-date (post (variable) id = 30)

Regards
Pat

#678518

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Well - Toolset also offers the shortcode [wpv-post-author].
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author

When you say connected user post - do you mean logged in user is post author of post (subscription)? if yes:
I would like to know where exactly you want to add the condition in question - on subscription post?

#678926

Pat

Hi Minesh,

The requested condition :
post (subcription) with author=connected user exists ? (there could be several post with the same author, but only one with the further date condition)
AND
post date (subscription) is between post (variable) start-date and post (variable) end-date (post (variable) id = 30)
Should be placed anywhere in the site, depending on thecontent that would be displayed for users having a vaild subscription and not for the others.

As you can see, the condition refers to 2 different posts of 2 different postypes ((subcription and variable)
Regards
Pat

#678927

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Yes - I understand that - but for which post we need to run those 2 condition.

Because - we need to first make sure for which post we need to fetch the post author and it's parent post's custom date fields. Do you have structure ready for this - are you able to share test case example - if yes: I can write the condition for you but I need access details for that.

#679129

Pat

Hi Minesh,

Not sure to understand. For me, this should be part of a conditional output, whatever the post / page where we are. This is just taking into account the current user and verify that this user is a post author of the postype "subscription" and that one of the found "subscription" posts is dated between 2 constants (that are recorded as custom fields of the post id=30 belonging to postype "variable").

Perhaps we can split into 2 :
- First, find the posts belonging to "subscription" havinf the current user as author
- Second, define is one of these posts date is between 2 variables

No sure that given you access to the site will help as nothing is alreading coded inthis area.
Regards
Pat

#683172

Dear Pat,

Minesh isn't available, I will take care of this thread.

For your questions:
1) First, find the posts belonging to "subscription" havinf the current user as author
You can create a view list "subscription" posts, filter by:
Post author is the same as the logged in user
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

2) Second, define is one of these posts date is between 2 variables
In the loop section of above view of 1), you can get the timestamp VALUE of the post publish date with view's short code:
[wpv-post-date format="U"]
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-date

get the custom date field timestamp value, with Types shortcode, for example
[types field="my-date" output="raw"][/types]

Use [wpv-conditonal] shortcode to compare above values, if it is satisfied, then display the result.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#704283

Pat

Hi Luo and thanks for your proposal.

That's OK like this, but I wasstill having an issue with it.
I want to display the conditional output inside a product page layout. The aim of the conditional is to display a WC Cred that is linked to the product (by the field product_id).
As the Views in your proposal refers to another postype (subscription), I cannot retrieve the product_id inside the Views;

So, I started another way to manage this issue : I have created a shortcode that looks if all conditions are OK and then, the conditional output refers to this shortcode.

Here is my code :
function check_user_adherent( ) {
$user = wp_get_current_user();
$startdate = get_metadata(175, 'debut-inscriptions', $single);
$enddate = get_metadata(175, 'debut-ete', $single);
$result = new WP_Query(
array(
'author' => $user->ID,
'post_type' => 'adhesion',
'post_status' => 'publish',
'date_query' => array(
'after' => $startdate,
'before' => $enddate,
'inclusive' => true
)
)
);
//return (count($result->posts));
if (count($result->posts) > 0) {
return 1;
}
else {
return 0;
}
}
add_shortcode( 'user_adherent', 'check_user_adherent' );

Regards
Pat