Skip Navigation

[Resolved] Conditional to determine current User’s horoscope sign

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use a conditional to display different content based on the current User's horoscope sign, based on a User birthdate custom field.

Solution:
Inside a View of Users, to access the current logged-in User's information you must use a nested View of Users. Create a new View of Users, filtered by specific Users, using a shortcode attribute filter "users". See the attached screenshot. Use the wpv-current-user shortcode to pass the current User's ID into the nested View filter. Here is an example using your portrait View:

<wpv-loop>
[wpv-view name="nested-view-slug" users="[wpv-current-user info='id']"]
</wpv-loop>

Add the following custom shortcode:

add_shortcode( 'is_month_day_between', 'is_month_day_between_func');
function is_month_day_between_func($atts)
{
  // shortcode attributes for timestamps start time, end time, and test time
  $start = $atts['start'];
  $end = $atts['end'];
  $test = $atts['test'];
  $result = 0;
  // make new timestamps using the current Year so we can compare
  $modStart = strtotime(date('d F, ', $start) . date('Y'));
  $modEnd = strtotime(date('d F, ', $end) . date('Y'));
  $modTest = strtotime(date('d F, ', $test) . date('Y'));
  // compare and return 0 or 1 if test is between start and end
  if(($modStart <= $modTest) && ($modTest <= $modEnd)){
    $result = 1;
  }
  return $result;
}

Register 'is_month_day_between' in Toolset > Settings > 3rd party Shortcode Arguments, then you can use it in a conditional that tests the User's birthday against a known range of dates.

[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')"]  This month and day is between start and end
[/wpv-conditional]
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-yourdatefieldslug']'] eq '1')" evaluate="false"]  This month and day is not between start and end
[/wpv-conditional]

To account for the first sign, which spans two years, you must use two conditionals that test if the date is between Dec 21 and Dec 31, or if the date is between Jan 1 and Jan 21.

This support ticket is created 6 years, 11 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
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)

Tagged: 

This topic contains 19 replies, has 3 voices.

Last updated by Pat 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#602269

Pat

Hello,

I have created user fields and wants to display them in a page thanks to a Views (and also, use them in a conditional output).
What I need here is to display the user fields related to the current user, the one who is looking at the page (knowing that many users can be connected in the same time).

I have tried this :

[types usermeta='date-de-naissance' user_current='true' style='text' format='m'][/types]

but this is not working (fields of all users are displayed !)

Regards
Pat

#602384

Hi, it sounds like you have placed this types field shortcode in a View's Loop between the wpv-loop tags. If that is a View of Users, then the field will be shown for each User in the loop. If you only want to show it once, place the types field shortcode outside the wpv-loop tags in your Loop Output editor, or in another Content Template.

If I have misunderstood, please include screenshots showing how you have placed this shortcode on your site. Thanks!

#602407

Pat

Hi Christian,

Thanks for your answer.
I tried your proposal and this does not change the issue.
Then, I placed the shortcode directly in a page, and then, the result is OK ???

The issue is that I need to use the shortcode for display, but also as a conditional output. Can I place a conditional output directly inside a page (as there is no shortcode builder available, I'm not sure this is something that is possible within Toolset !)

Here is my code in Views (Views name is portrait):
[wpv-layout-start]
[wpv-items-found]
[types usermeta='date-de-naissance' style='text' format='m' user_current='true'][/types]
<!-- wpv-loop-start -->
<wpv-loop>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]

The code in the page :
[types usermeta='date-de-naissance' style='text' format='m' user_current='true'][/types]
[wpv-view name="portrait"]

The result of the page display is :

01 => month of the current user (right)
09 => month of the user with the lowest ID) (wrong)

And the condition I need to place :
[wpv-conditional if="([types usermeta='civilite' user_current='true'][/types]' eq 'Homme' ) AND ([types usermeta='date-de-naissance' style='text' format='m' user_current='true'][/types]' eq '01' )"]

I placed this condition in both the page and the Views, but none is working !

Regards
Pat

#602740
Screen Shot 2018-01-02 at 1.22.00 PM.png

Inside a View of Users, to access the current logged-in User's information you must use a nested View of Users. Create a new View of Users, filtered by specific Users, using a shortcode attribute filter "users". See the attached screenshot. Use the wpv-current-user shortcode to pass the current User's ID into the nested View filter. Here is an example using your portrait View:

<wpv-loop>
[wpv-view name="nested-view-slug" users="[wpv-current-user info='id']"]
</wpv-loop>

In the Loop Output editor for the nested View of Users:

<wpv-loop>
  Current User's birth month: [types usermeta="date-de-naissance" format="m"][/types]
  [wpv-conditional if="( '[types usermeta='date-de-naissance' format='m'][/types]' eq '01' )"]
    Current User's birth month = 01
  [/wpv-conditional]
</wpv-loop>
#602929

Pat

Hi Christian,

Seems the current user info is OK now. In fact, I have just added the [wpv-view name="nested-view-slug" users="[wpv-current-user info='id']"] inside the page and it's working fine.

Now, I still have an issue with the conditional. What I want is to compare if a date is between D1D1/M1M1 and D2D2/M2M2
Year is not important. So I tried this :

[wpv-conditional if="(( '[types usermeta='date-de-naissance' format='m'][/types]' gte '07' ) AND ( '[types usermeta='date-de-naissance' format='d'][/types]' gte '23' )) AND (( '[types usermeta='date-de-naissance' format='m'][/types]' lte '08' ) AND ( '[types usermeta='date-de-naissance' format='d'][/types]' lte '23' ))"]
Current User's birth month = 08 : Lion
[/wpv-conditional]

Here, is it defined to check if the date is between July, 23rd and August, 23rd.

But it's not working as desired.
Could you help please
Regards
Pat

#603104

This conditional isn't going to work because the logic is not correct. For example, the second comparison clause is
day >= 23
but the fourth comparison clause is
day <= 08
These two are mutually exclusive, so no results will ever match because the day cannot be >= 23 and <= 8. It's not possible. So that means you have to introduce more conditionals testing the months and days together. Here's the idea:

if ( (month==7 and day >=23) or (month==8 and day<=23) )

It might be possible to do this in a Views conditional, but it's messy and hard to maintain. If I were doing this, I would consider writing a custom shortcode that compares dates using timestamps and PHP.

#603151

Pat

Hi Christian,

I'm OK with PHP but not sure that timestamps will be helpful as we are speaking of only a part of the date (if we need to consider all yearly potential occurences, that would be a mess !!!).
Any idea ?
Regards
Pat

#603192

I understand that a timestamp will require some manipulation because the years can be different. To compare them, you can convert all 3 timestamps to date strings in the same year. Then convert those back to timestamps and compare those converted timestamps. Something like this:

add_shortcode( 'is_month_day_between', 'is_month_day_between_func');
function is_month_day_between_func($atts)
{
  // shortcode attributes for timestamps start time, end time, and test time
  $start = $atts['start'];
  $end = $atts['end'];
  $test = $atts['test'];
  $result = 0;
  // make new timestamps using the current Year so we can compare
  $modStart = strtotime(date('d F, ', $start) . date('Y'));
  $modEnd = strtotime(date('d F, ', $end) . date('Y'));
  $modTest = strtotime(date('d F, ', $test) . date('Y'));
  // compare and return 0 or 1 if test is between start and end
  if(($modStart <= $modTest) && ($modTest <= $modEnd)){
    $result = 1;
  }
  return $result;
}

Then register the "is_month_day_between" shortcode in Toolset > Settings > Frontend content > 3rd party shortcode arguments. Now you can use the shortcode in a conditional with any combination of known timestamps and custom field date values:

[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')"]  This month and day is between start and end
[/wpv-conditional]
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-yourdatefieldslug']'] eq '1')" evaluate="false"]  This month and day is not between start and end
[/wpv-conditional]
#603775

Pat

Hi Christian,

Thanks for the proposal.
I have tested and it's working but only when the date (here wpcf-date-de-naissance filed) to check is before 1970 (ie : when Unix time is negative).
In the other case, nothing is displayed.
Any idea?
Regards
Pat

#604085

Okay let's figure that out. Can you turn on server logs and add some log statements? Please add these log statements to your code and share the results. I'd like to see what's going on.

add_shortcode( 'is_month_day_between', 'is_month_day_between_func');
function is_month_day_between_func($atts)
{
  // shortcode attributes for timestamps start time, end time, and test time
  $start = $atts['start'];
  $end = $atts['end'];
  $test = $atts['test'];
  error_log('start, end, test: ' . $start . ', ' . $end . ', ' . $test);
  $result = 0;
  // make new timestamps using the current Year so we can compare
  $modStart = strtotime(date('d F, ', $start) . date('Y'));
  $modEnd = strtotime(date('d F, ', $end) . date('Y'));
  $modTest = strtotime(date('d F, ', $test) . date('Y'));
  
  error_log('modStart, modEnd, modTest: ' . $modStart . ', ' . $modEnd . ', ' . $modTest);
  // compare and return 0 or 1 if test is between start and end
  if(($modStart <= $modTest) && ($modTest <= $modEnd)){
    $result = 1;
  }
  return $result;
}

Please also turn on debugging in your conditional and share the output onscreen:

[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')" debug="true"]
#605858

Pat

Hi Christian,

Sorry to have been long to answer.
I have placed the desired code and when I try to display the conditional, here is the message I'm getting :

Warning: date() expects parameter 2 to be long, string given in /my_site/wp-content/themes/toolset-starter/functions.php on line 88

The line 88 is :
$modTest = strtotime(date('d F, ', $test) . date('Y'));

Regards
Pat

#606077

Okay it sounds like something is going on with the "test" attribute value in your conditional. Please copy + paste the entire conditional here for me to review. Please copy the wpv-post-field code from your conditional and paste it just before the conditional so we can debug it. Like this:

Test date de naissance: [wpv-post-field name='wpcf-date-de-naissance'] 
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-post-field name='wpcf-date-de-naissance']'] eq '1')" debug="true"]

What does this show on the front-end of your site?
Then, please be sure to copy + paste the relevant server logs here. Not just the warnings, please also include the "start, end, test" lines and "modstart, modend, modtest" lines.

#606081

Pat

Hi Christian,
Thanks for your time.

SOrry, but I'm getting exactly the same result.

But, I have to highlight something :
"date-de-naissance" is not a postfield but a user field. Perhaps this could be the cause of the problem?
Let me know
Regards
Pat

#606113

Okay since it's a user field, please use this code instead:

Test current user ID: [wpv-current-user info="id"]
Test user ID: [wpv-user field="ID"]
Test date de naissance: [wpv-user field='wpcf-date-de-naissance']
Test current user date de naissance: [wpv-user field='wpcf-date-de-naissance' id='[wpv-current-user info='id']']
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-user field='wpcf-date-de-naissance']'] eq '1')" debug="true"]
#606147

Pat

Hi Christian,

That's better interms of error not showing now.
But, concerning the function, this is stil not working fine.

It was working if the "date-de-naissance" was August, 10th 1957
With this conditional :
[wpv-conditional if="([is_month_day_between start='1498247543' end='1440359543' test='[wpv-user field='wpcf-date-de-naissance']'] eq '1')" debug="true"]

Now, if I'm using a January, 1st 2000 with this condition :
[wpv-conditional if="([is_month_day_between start='945817200' end='948495599' test='[wpv-user field='wpcf-date-de-naissance' user_current='true']'] eq '1')"]
Here, I'm using 945817200 = Tue, 21 Dec 1999
And 948495599 = Fri, 21 Jan 2000

So, normally, it should work !

Regards
Pat