prashantcs053
7/3/2017 - 1:25 PM

update CPT on 404 page with only one time update url/404/?update_retailer=yes

update CPT on 404 page with only one time update

url/404/?update_retailer=yes

add_action("wp", "wp_update_all_retailer");

function wp_update_all_retailer() {
    if(is_404() && isset($_GET['update_retailer']) && $_GET['update_retailer'] == 'yes') {      
        $args = array(
            'post_type' => 'retailers',
            'nopaging' => true,
            'post_status' => 'any',
            'fields' => 'ids',
            'order' => 'asc'
        );
        $args['meta_query'] = array(
           'relation' => 'OR',
            array(
             'key' => '_state_updated_auto',
             'compare' => 'NOT EXISTS' // doesn't work
            ),
            array(
             'key' => '_state_updated_auto',
             'value' => 'yes',
             'compare' => '!='
            )
        );
        $query = new WP_Query($args);
        if($query->found_posts > 0) {
            $h=1;
            foreach ($query->posts as $value) {
                $post_id = $value;
                update_search_filter($post_id);
                update_post_meta($post_id, '_state_updated_auto', 'yes');
                echo $h.'. '.$post_id.' updated.<br/>';
                $h++;
            }
            exit;
        }
    }
}