0

I want to pass the $job_id variable to the expire_job_featured function (for wp single cron event) but I don't know how can I do that.

This is the full code of setting job's post meta value "featured" to 1 and then I want to schedule a single cron event to make the "featured" meta value to 0 after an hour. But I can't pass the $job_id variable to the expire_job_feature function.

    add_action( 'init', function() {
    $job_id = filter_input( INPUT_GET, 'feature_job', FILTER_VALIDATE_INT );

    if ( null === $job_id ) {
        return;
    }

    update_post_meta( $job_id, 'featured', 1 );
    wp_schedule_single_event( time() + 3600, 'job_featured_expire' );
    
});

function expire_job_featured($job_id) {
   update_post_meta( $job_id, 'featured', 0 );
}
add_action( 'job_featured_expire','expire_job_featured' );
5
  • What are you trying to do? What is the problem this solution is trying to solve? ( and why didn't you ask about that instead? I see there are some fundamental misunderstandings here about what functions and filters are, as well as how cron jobs work
    – Tom J Nowell
    Sep 11, 2020 at 16:15
  • I trying to pass the $job_id to the "expire_job_featured" function... because currently the $job_id is doesn't work in that function. returns nothing
    – Roland
    Sep 11, 2020 at 16:19
  • Did you read the documentation for wp_schedule_single_event? Specifically the 3rd argument it takes. Also you should use a taxonomy term not a post meta field if you're going to do a meta query. Searching for the jobs that are featured will become a very expensive and slow query to run if you use a meta_query, and it will get progressively slower as the site gets larger very quickly
    – Tom J Nowell
    Sep 11, 2020 at 16:32
  • Then I need to using like this? wp_schedule_single_event( time() + 3600, 'job_featured_expire', $job_id ); Well, in this way still didn't change the post's meta value to 0.
    – Roland
    Sep 11, 2020 at 16:36
  • That's not how the 3rd parameter of that function works
    – Tom J Nowell
    Sep 11, 2020 at 16:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.