Custom Fields
Re-Envisioned
Support MB Views Assign default value to DATE field?

  • Creator
    Topic
  • #21822
    Resolved EddyPiV
    Participant

    Hi,

    Is there a way to define a default value to the DATE field?
    What I want is to assign a date field to a cpt, make it hidden and assign a default value of today+1.

    Intention is in a view to skip the posts with the assigned date & time < NOW.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #21831
    Long Nguyen
    Moderator

    Hi,

    We can use the field setting std to add the default value for the date field. Please add this sample code to the file functions.php in the theme folder or use Code Snippets.

    add_filter( 'rwmb_meta_boxes', function() {
            $date = new DateTime('+1 day'); // here
            $meta_boxes[] = array(
                'title'      => 'Meta Box Title',
                'post_types' => ['post', 'page'],
                'id' => 'meta-box-id',
                'fields' => array(
                    array(
                        'name'       => 'Date picker',
                        'id'         => 'field_id',
                        'type'       => 'date',
    
                        // Date picker options. See here http://api.jqueryui.com/datepicker
                        'js_options' => array(
                            'dateFormat'      => 'yy-mm-dd',
                            'showButtonPanel' => false,
                        ),
    
                        // Display inline?
                        'inline' => false,
    
                        // Save value as timestamp?
                        'timestamp' => false,
                        'std' => $date->format('yy-m-d'), // // use the date format as js_options
                    ),
                ),
            );
        
        return $meta_boxes;
    } );

    For more information, please follow the documentation.
    https://docs.metabox.io/field-settings/
    https://docs.metabox.io/fields/date/

    #21873
    EddyPiV
    Participant

    Actually, I found out that what I wanted to do wouldn’t work: I was trying to set the expiration date fields of the Post Expirator plugin, but there is more needed to get it to work, probably setting a cron job.

    So my question is now a bit different.
    I am now skipping the posts that are older than 24 hours.

    I included this if statement: {% if {{ post.date }} > strtotime(‘-1 day’) %},
    but WordPress didn’t like it… critical error on the website.

    Here is the view: https://share.getcloudapp.com/RBuOpJkA

    #21878
    Long Nguyen
    Moderator

    Hi,

    To run a PHP function in View, please use the proxy mb. The code should be:

    mb.strtotime('-1 day')

    #21880
    EddyPiV
    Participant

    Hi,

    I know, the devil is in the details… I should have added the mb. prefix…
    But it doesn’t resolve the critical error.

    Deleting the “if” construction avoids the critical error, so it is really in this statement.

    https://share.getcloudapp.com/nOukxRmP

    #21881
    Long Nguyen
    Moderator

    Hi,

    Two curly brackets {{ display a value on the frontend }} is to display a value on the frontend. Please remove it {% if post.date > mb.strtotime('-1 day') %}.

    #21885
    EddyPiV
    Participant

    That indeed resolves the critical error.

    And some new questions arise…

    Question 1:
    There are 2 posts found, 1 with date today, 1 with date 3 days ago.
    Yet, the post.date for both (according to the view) are shown as today. Why is that???
    Print of database: https://share.getcloudapp.com/X6uN152L
    Result of the View: https://share.getcloudapp.com/L1uJA65q
    The View-code: https://share.getcloudapp.com/xQuL2Og7

    Question 2:
    At least 1 post should be displayed with its details, but there are no posts displayed.
    Considering the post.date for both showing today, both should be displayed.
    Why are no posts displayed?
    Something with stored formats? How to formulate the condition correct?

    #21889
    Long Nguyen
    Moderator

    Hi,

    As I said in this topic https://metabox.io/support/topic/how-to-show-all-posts-for-specific-taxonomy/page/2/#post-21633.

    With the custom query, we have to use the variable of the WP_Post object. To get the post date, use post.post_date. For more information, please follow the documentation https://developer.wordpress.org/reference/classes/wp_post/.

    #21890
    EddyPiV
    Participant

    Yes, that was it. The format mistake I already figured out.
    Thanks, let’s close this thread.

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