0

My question is - I have a CPT Post name Company (CF Value - Google) and a Page titled Role (CF V - Engineer). So I want to auto-update with Post published time and date to company post and Role page if published post custom field value match to Google and Engineer.

For example -

  • A Job Post published at 11:30:21PM, 13/09/2021, has two Custom Fields Title Company and Role which has Google and Engineer values.

  • Already published CPT Post Company has a post Google Jobs which has similar CF values Google.

  • Similarly already published page Engineer Jobs which has CF value Engineer.

So my all condition is satisfied because CF value matched, now the code should update Engineer Job (Page), Google Jobs (Post) date and time with recent published post date, time.

Any reference code or help will be appreciated, I want to implement code in my functions.php file.

My Code (Not Working)

function my_cron_schedules($schedules){
    if(!isset($schedules["5min"])){
        $schedules["5min"] = array(
            'interval' => 1*60,
            'display' => __('Once every 5 minutes'));
    }
    if(!isset($schedules["30min"])){
        $schedules["30min"] = array(
            'interval' => 30*60,
            'display' => __('Once every 30 minutes'));
    }
    return $schedules;
}
add_filter('cron_schedules','my_cron_schedules');
wp_schedule_event(time(), '5min', 'my_hourly_event', $args);

//add_action('init','my_hourly_event');
function my_hourly_event() {
    $the_query = get_posts( 'post_type=post' ); 
    foreach($the_query as $single_post) {
        $id=$single_post->ID;
        $ad_close_date=get_post_meta($id, 'Validdate', true );
        if($ad_close_date!=''){
            $today=date("j F Y");
            if($ad_close_date<$today){
                $update_post = array(
                'ID'            => $id,
                'meta_key' => 'Location',
                'publish_date'  =>  get_the_time( '', $post->ID ),
                'post_type' =>  'jobs',
                'meta_value' => get_post_meta(get_the_ID(), 'Location',true));
                wp_update_post($update_post);
            }   
        }
    }
}

1 Answer 1

0

Try using hook on update post meta for post update date. Something like code below, just change the company_title_meta and company_role_meta to your correct meta key.

If something does not work, send a comment and I will try to help you

add_action( 'updated_post_meta', 'on_post_update_after_post_meta', 10, 4 );

function on_post_update_after_post_meta( $meta_id, $post_id, $meta_key, $meta_value )
{
    if ( in_array($meta_key, ['company_title_meta','company_role_meta']) ){
        $time = time();
        $my_post = array(
            'ID'            => $post_id,
            'post_date'     => date( 'Y-m-d H:i:s', $time ),
            'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $time ),
        );
        wp_update_post( $my_post );
    }
}
1
  • I tried your code but not working, I have nothing changed in my previous code and add you new code. Changed company_title_meta -> Company and company_role_meta -> Role. I want to update the published date where the condition is satisfied. Didn't want to update custom field value or any meta. Nov 5, 2021 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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