Change a post date in a Wordpress plugin which is based on meta post variable

Change a post date in a Wordpress plugin which is based on meta post variable

This article shows how to change a date in a Wordpress plugin automatically after a custom post type is created or modified.

For a customer I had a case where I had build a plugin with a custom post type that contained a date field in the meta data. The problem is that sorting on meta data is not as easy as sorting on the general post fields. So I came up with the idea that every time when a custom post type post is created or modified I change the publish date to the same date in the meta field.

This sounds as redundant, well maybe it is. But the custom post type is not intended to be published as an entity on its own and the customer might get confused about the essence of the date.

In the image it is about the field 'Datum zaak', which contains here 13-6-2013. That shoud be placed in the published date of Wordpress automatically after an insert or update.

How it is done

What you have to know in advance:

  • custom post type: vscc-zaak
  • metadata: vscc_zaak_metadata
  • datefield in metadata: datum_zaak

The following code is a crude excerpt of the filters and functions required within the plugin file:

<?php

...

class My_Special_Plugin
{
    const PLUGIN_NAME = 'my_special_plugin';
    const PLUGIN_ID = 'my-special-plugin';
    const PLUGIN_TITLE = 'My Special Plugin';

    function __construct()
    {

        ... everything you require here ...

        // Filters to setup the automatic change of the publish date
        add_filter( 'wp_insert_post_data' , [$this, 'update_post_date'] , '99', 2 );
        add_filter( 'wp_update_post_data',  [$this, 'update_post_date'] , '99', 2 );

        ... everything else ...
    }

    function update_post_date( $data , $error ) {

        // first check if the post_type is the right one
        if (array_key_exists('post_type', $_POST) && $_POST['post_type'] === 'vscc-zaak') {
            // check if the correct metadata POST variable is available
            if (array_key_exists('vscc_zaak_metadata', $_POST)) {
                $metaData = $_POST['vscc_zaak_metadata'];
                // check if the field to use is in the metadata array that came with POST
                if (array_key_exists('datum_zaak', $metaData)) {
                    // format is '14/10/2019', but that doesn't matter much, for DateTime will figure this out
                    $datumZaak = $metaData['datum_zaak'];
                    // format is 'yyyy-mm-dd hh:mm:ss', that is Wordpress itself
                    $datumPost = $data['post_date'];

                    // a bit extended for clearity
                    $dateOne = new DateTime($datumPost);
                    $dateTwo = new DateTime($datumZaak);

                    // we use the DateTime->modify function
                    $dateOne->modify($dateTwo->format('Y-m-d'));

                    // We apply a change to post_date and post_date_gmt to the same value
                    $data['post_date'] = $dateOne->format('Y-m-d H:i:s');
                    $data['post_date_gmt'] = $dateOne->format('Y-m-d H:i:s');
                }
            }
        }

        // finally we return the $data and Wordpress will take it over from there
        return $data;
    }

    ... other functions in this plugin ....

}

$My_Special_Plugin = new My_Special_Plugin();

More from same category