0

I have custom post type with some custom fields. And "date" field and "status" field are one of them.

Here am trying to update meta_value of "status" filed based on comparison of "date" field with current date.

  • What have you tried so far? How are you creating the "date" field and what format is it stored in? – WebElaine Feb 26 '18 at 19:21
1

UPDATE: 2/28/18

Since you need to update all the posts in a particular taxonomy, it sounds like you need to read up on how WP_Query() works.

Here's a simple loop to get you started that will fetch all posts in a taxononmy, and update them if the "now is later than then" condition is met.

$nitish_args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'YOUR TAXONOMY HERE',
        ),
    ),
);

$nitish_query = new WP_Query( $nitish_args );

if( $nitish_query->have_posts() ){
    // Get "current" unix timestamp
    $now = time();

    while( $nitish_query->have_posts() ){
        $nitish_query->the_post();

        // Get "date" meta field as unix timestamp
        $then = strtotime( get_post_meta( $post->ID, 'date', true ) );

        if( $now > $then ){
            // $now is later than $then, update post.
            update_post_meta( $post->ID, 'status', 'NEW VALUE' );
        }
    }
    wp_reset_postdata();
}

Sounds like a simple if condition. You can run this on:

  • Cron
  • Post Views (add to single.php or use the is_singular() or is_single() functions in functions.php)
  • On post update with the save_post() action
  • In a WP_Query or archive template loop.
  • Doubtful, but even any time the site is loaded (directly in functions.php - not likely to be the place, but it's possible depending on what you need to accomplish)

It really depends on your needs, but here's something to get you started:

// Get "date" meta field as unix timestamp
$then = strtotime( get_post_meta( $post_id, 'date', true ) );

// Get "current" unix timestamp
$now = time();

if( $now > $then ){
    // $now is later than $then, update post.
    update_post_meta( $post_id, 'status', 'NEW VALUE' );
}

Basically you just need to get the unix timestamp of the date field, compare that to the current unix timestamp with the time() function, and then see if "now" is greater than "then", and if so - update the post.

  • Thanks @Xhynk, but it seems like it will only work for particular post_id. I want to update all post_id of particular taxonomy. – Nitish Paswan Feb 28 '18 at 5:28
  • Are you familiar with WP_Query? I'm on mobile so I can't really type a full one completely, but you can query all posts in a taxonomy, and then run this code in it instead of displaying posts – Xhynk Feb 28 '18 at 6:11
  • No i don't know about Wp_query. – Nitish Paswan Feb 28 '18 at 6:13
  • I've updated my answer. WP_Query() is something you should read up on. The new code sample fetches all posts in that taxonomy, and then compares the date field to the current time and then updates the status field if true. – Xhynk Feb 28 '18 at 16:51
  • Your solution works, but all of my posts of that taxonomy getting "inactive". Now, I think am near around my solution. Many thanks for your help :). – Nitish Paswan Mar 1 '18 at 9:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.