Populate Custom Field with Another Custom Field's Data

Support MB Group Populate Custom Field with Another Custom Field's DataResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #39421
    PetePete
    Participant

    Hi all,

    This might be a super weird question, and there may well be a better way to do this but here's what I'm looking to accomplish:

    The Setup
    There's a plugin for handling events, it's very basic, which is exactly what I need. In that plugin, there's a custom field "event_date". This field spits out a Unix timestamp. I have a function that can convert that timestamp to human readable format (Month, Year) for use in Bricks Builder:

        // Return with {echo:xxxx_event_human_date}
        function xxxx_event_human_date() {
            $the_event_start_date = get_post_meta(get_the_ID(), 'event_date', true);
            $the_event_start_date = date('F, Y', $the_event_date);
        return $the_event_start_date ?: '';
        }

    What I want to do is add that human readable value to custom field created with Metabox, which would (in my brain) then create a text value of Month, Year.

    This text value would then be used with WP Grid Builder in order to filter events by their month and year value.

    The Asks
    1) Will this even work?
    2) How can I accomplish this auto conversion and population into the Metabox field, if it is possible?

    I'm VERY new to PHP, so I have no idea what's possible and what's not.

    Cheers!
    Pete

    #39493
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Hi,
    I dint check it, anyway try this

    
     function xxxx_event_human_date() {
            $the_event_start_date = get_post_meta(get_the_ID(), 'event_date', true);
            $the_event_start_date = date('Y-m-d', $the_event_start_date); // changed the variable and date format
        return $the_event_start_date;
        }
    //in the required place call the function like
    echo xxxx_event_human_date();
    
    #39506
    PeterPeter
    Moderator

    Hello,

    If you want to output the date in human-readable format, you can use the setting save_format which uses the PHP format for the date. For example:

    [
        'name'        => __( 'Date', 'your-text-domain' ),
        'id'          => $prefix . 'date',
        'type'        => 'date',
        'save_format' => 'F, j Y',
        'js_options'  => [
            'dateFormat' => 'dd-mm-yy',
        ],
    ],

    Read more on the documentation https://docs.metabox.io/fields/date/

    If you want to set a field value, please follow this documentation https://docs.metabox.io/functions/rwmb-set-meta/

    #39619
    PetePete
    Participant

    Thanks guys! I got it sorted in the end with the following:

    function XXXX_readable_month_year_event($post_id) {
        if (get_post_type($post_id) == 'event') {
            $event_date = get_post_meta($post_id, 'event_date', true);
            $readable_date = date('F, Y', $event_date);
            update_post_meta($post_id, 'event_readable_my', $readable_date);
        }
    }
    add_action('save_post', 'XXXX_readable_month_year_event');

    What that does in order is:

    1) Checks the post type is "event"
    2) Gets the field "event_date"
    3) Turns "event_date" into human readable format Month, Year
    4) Adds the readable format as text to the Metabox cusom field "event_readable_my"
    5) Runs on save

    I think I explained badly what I needed to happen originally, but I got there in the end lol

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.