Skip Navigation

[Resolved] How to get field value in CPT using PHP?

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

Problem:

In a single "member's profile" post, there is a custom date field "expire-date", which is created with Types plugin, you are going to get the "Year" value from "expire-date" field.

Solution:

You can try get the timestamp value of "expire-date" field with function get_post_meta(), for example:

$expire_date_timestamp = get_post_meta(get_the_ID(), 'wpcf-expire-date', true);

Then you can get the year value by timestamp using PHP function date, like this:

$expire_date_year = date('Y', $expire_date_timestamp);

Relevant Documentation:

http://php.net/manual/en/function.date.php

https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created 5 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
- 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)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1160537

Luo -
This issue is revisiting from issue: https://toolset.com/forums/topic/how-to-update-types-default-value-for-custom-field-programatically/

I got the logic working the way I want, but I now realize I need to start the options with whatever the current setting is for a given member's profile expiration date. Unfortunately, some people pay a renewal twice, and my client accepts that, so that member's account does not expire for two years rather than one. That means I cannot assume the member's current expiration date is expiring in December of the current year. It could also be expiring December, next year. Or in a rare case, the next year after that one.

So, I think the best way to handle that would be to get the current value (for instance, "2018") from the "expire-date" field instead of using the acual year from date('Y'); and then set the options array for Select to be: "2018, 2019, 2020, 2021, 2022". I know I originally had 9 options, but that many is not necessary. Five should be enough. So next year, the options array will become: "2019, 2020, 2021, 2022, 2023".

How can I use PHP to get the current value of the "expire-date" field in a member's profile CPT? I know how to do this in Javascript, but not sure how to do it in PHP. Then I can just add the next 4 sequential years to complete the options array.

Here is my current code which I modified from the code you gave to me in Reply ##1155229 :

add_filter( 'wpt_field_options', 'dynamic_select', 10, 3);
  
function dynamic_select( $options, $title, $type ){        
    switch( $title ){        
        case 'Expiration date': 
            $this_year = date('Y');
            $options = array();
            $i = 0;
            for($i; $i<5; $i++){                           // 2018, 2019, 2020, 2021, 2022  (for this year)
                $options[] = array(
                    '#value' => $this_year + $i, 
                    '#title' => $this_year + $i,
                );
            }
            break;
    }
    return $options;
}

Thanks,
Jeff Safire
-------------------

#1160544

Dear Jeff,

I assume we are talking about this case,
You are editing a single "member's profile" post, there is a custom date field "expire-date", which is created with Types plugin, you are going to get the "Year" value from "expire-date" field.

If it is, you can try get the timestamp value of "expire-date" field with function get_post_meta(), for example:

$expire_date_timestamp = get_post_meta(get_the_ID(), 'wpcf-expire-date', true);

Then you can get the year value by timestamp using PHP function date, like this:

$expire_date_year = date('Y', $expire_date_timestamp);

More help:
hidden link
https://developer.wordpress.org/reference/functions/get_post_meta/

#1160563

My issue is resolved now. Thank you!

#1160956

You are welcome.