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

  • Creator
  • #21822
    Resolved EddyPiV


    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
  • #21831
    Long Nguyen


    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(
                        'name'       => 'Date picker',
                        'id'         => 'field_id',
                        'type'       => 'date',
                        // Date picker options. See here
                        '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.


    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 {{ }} > strtotime('-1 day') %},
    but WordPress didn't like it... critical error on the website.

    Here is the view:

    Long Nguyen


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

    mb.strtotime('-1 day')



    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.

    Long Nguyen


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


    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 for both (according to the view) are shown as today. Why is that???
    Print of database:
    Result of the View:
    The View-code:

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

    Long Nguyen


    As I said in this topic

    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


    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.