Categories
Technology

How to Change the Date From Custom Custom Field in Wordpress

Hi πŸ‘‹

we will use WordPress get_the_date() Hook.

I will explain both. If you have created meta field for date post_custom_date. OR using ACF created field with name post_custom_date.

Inspiration

When I was developing the site with Divi theme there is a custom post listing module but I don’t want the date of publishing post instead i want my custom date which I have created with ACF.

So I have searched and found this solution Enjoy. πŸŽ‰

Put this code in functions.php file.

For custom meta Without Acf


<?php
    /**
    * @here 10 is Priority and 3 is Number of arguments to be passed
    */
    add_action( 'get_the_date','custom_date_callback', 10 , 3 );
    function custom_date_callback( $the_date, $format, $post ){
        if( get_post_type() === "custom_post_type" && is_singuler("custom_post_type") ){
            $date =  sprintf( esc_html( get_post_meta('post_custom_date', $post->ID) ) );
            return $date;
        }
    }
?>

For custom meta With Acf


<?php
    /**
    * @here 10 is Priority and 3 is Number of arguments to be passed
    */
    add_action( 'get_the_date','custom_date_callback', 10 , 3 );
    function custom_date_callback( $the_date, $format, $post ){
        if( get_post_type() === "custom_post_type" && is_singuler("custom_post_type") ){
            $date =  sprintf( esc_html( get_field('post_custom_date', $post->ID) ) );
            return $date;
        }
    }
?>

checkout all hooks hooks


For Refrence

https://developer.wordpress.org/reference/functions/get_the_date/ https://developer.wordpress.org/reference/functions/the_date/


If it is helpfulΒ and saves your valuable Time ⏱ please show your support πŸ‘‡.

Buy Me A Coffee

Buy me A Coffee, Thank you and canva ( For Beautiful designs ). Thanks for the reading πŸ‘.