• I’m creating a custom plug-in for my WordPress site that will run a query. The plugin will run via WordPress’ wp_schedule_event function to run daily. This query will act upon the engine post type (engine) with a custom field named wpcf-engine-days-to-go. Currently, my code is not working (see below)

    Goal
    I want to update the post_status to ‘draft’ when a custom field value, wpcf-engine-days-to-go reaches ‘0’. The post_status value is located in the wp_posts table, and the wpcf-engine-days-to-go is located in the wp_postmeta table.

    Question
    What is the correct query to update a value in one table based upon another, specifically in my example?

    register_activation_hook(__FILE__, 'tdengine_my_activation');
    add_action('tdengine_my_daily_event', 'tdengine_do_this_daily');
    
    function tdengine_my_activation() {
        wp_schedule_event(time(), 'daily', 'tdengine_my_daily_event');
    }
    
    function tdengine_do_this_daily() {
        global $wpdb;
        $query = "UPDATE <code>" . $wpdb->prefix . "posts</code> SET <code>post_status</code>=<code>draft</code> WHERE <code>meta_key</code> = 'wpcf-engine-days-to-go' = 0 ";
        $wpdb->query($query);
    }
    
    register_deactivation_hook(__FILE__, 'tdengine_my_deactivation');
    
    function tdengine_my_deactivation() {
        wp_clear_scheduled_hook('tdengine_my_daily_event');
    }

  • The topic ‘MySQL update wp_posts based on wp_meta value?’ is closed to new replies.