$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How do you update post date (year only) in a separate custom field?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How do you update post date (year only) in a separate custom field?

matteradmin9PV0评论

I would like to update all my posts with a custom field that stores the year the post was published.

The "year" value will obviously be retrieved from the post's date meta data.

I will then use the value stored to query posts with a certain plugin.

So far, I have this, but not sure where this code should go and if it needs modification!

$year_published = get_the_date( 'Y' ); 
update_post_meta(get_the_ID(), 'year_date', '$year_published' );

I would like to update all my posts with a custom field that stores the year the post was published.

The "year" value will obviously be retrieved from the post's date meta data.

I will then use the value stored to query posts with a certain plugin.

So far, I have this, but not sure where this code should go and if it needs modification!

$year_published = get_the_date( 'Y' ); 
update_post_meta(get_the_ID(), 'year_date', '$year_published' );
Share Improve this question edited Dec 3, 2018 at 15:22 Qjoey asked Dec 3, 2018 at 12:28 QjoeyQjoey 11 bronze badge 4
  • There are a few approaches to this, but firstly, you will need to fix your code with: update_post_meta( get_the_ID(), 'year_date', $year_published ); – Christine Cooper Commented Dec 3, 2018 at 12:54
  • Thanks Christine. could you suggest one short approach? I would like to update all existing and future posts. Preferably without using a plugin? – Qjoey Commented Dec 3, 2018 at 12:58
  • 1 I suggest hooking into save post and storing the custom meta data. And also looping through all existing posts and updating the value... – Christine Cooper Commented Dec 3, 2018 at 14:12
  • So I managed to loop through and update the values... However, when I inspect post meta fields, both the month and year are only storing empty strings ' ' instead of the actual value. Please see my answer below and help point out any edits to fix this. – Qjoey Commented Dec 3, 2018 at 21:38
Add a comment  | 

1 Answer 1

Reset to default 0

So I was first looking for a way to loop through the existing posts, as suggested in Christine's comment. Based on this answer, I created a basic plugin that runs the loop upon activation.

// Run the loop when the plugin is activated
register_activation_hook(__FILE__, 'update_my_metadata');
function update_my_metadata(){
$args = array(
    'post_type' => 'post', // Only get the posts
    'post_status' => 'publish', // Only the posts that are published
    'posts_per_page'   => -1 // Get every post
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
    // Run a loop and update every meta data
    $month=get_the_date('M');
    $year=get_the_date('Y');
    update_post_meta($post->ID, 'month-field', $month); echo $month;
    update_post_meta($post->ID, 'year-field', $year); echo $year;
 }
}

However, when I inspect post meta fields, both the month and year are only storing empty strings '' instead of the actual date value.

Any suggestions?

Post a comment

comment list (0)

  1. No comments so far