get_posts query results out of order but shows correct in php snippets plugin

Question

I have a small plugin made for a custom site where a new word is to be picked via wp cron . The oldest (lowest ID #) custom post type should be selected in get_posts and returned. However, it always will return and select the newest (largest post ID ) of that custom type.

What is weird is that if I try the same query in a XYZ PHP Code ( plugin that allows you to make short codes with PHP in the dashboard ), the correct result will be returned.

here is the query I’ve tried among many. I’ve also tried ‘ID’, ‘date’, ‘DATE’, all order of ‘asc’ and it isn’t working. I also manually deleted previous wp_schedule_event when trying a new version of the plugin via WP Crontrol.

//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'wordss_cron_hook' ) ) {
    wp_schedule_event( strtotime('2019-10-31 03:58:00'), 'daily', 'wordss_cron_hook' );
}

///Hook into that action that'll fire daily
 add_action( 'wordss_cron_hook', 'wordss_cron_function' );

//create your function, that runs on cron
function wordss_cron_function() {
    //your function...
     $args = array('post_type' => 'word',
            'post_status' => 'publish',
            'category__not_in'=>array(3,4),
            'meta_key'      => 'word_is_active',
            'meta_value'    => true,
            'suppress_filters' => true,
            'orderby' => 'id',
            'order '=>'asc',
            'posts_per_page' => 1
            );
        $posts = get_posts($args);



              if (count($posts) > 0) {
             $postid=$posts[0]->ID;
                $posttitle = $posts[0]->post_title;

                    $imageid=get_post_meta($postid,'word_image',true);
                $postimage=$imageid['guid'];

                $obj['secondpost'] = (object) array('posttitle' => $posttitle,'postid'=>$postid,'postimage'=>$postimage);
                wp_set_post_categories($postid,array(3));
                update_option('saved_day_post', $obj);
                wpfc_clear_all_cache(true);



        }
}
0
, , CI_Guy 2 years 2019-10-31T09:51:13-05:00 0 Answers 95 views 0

Leave an answer

Browse
Browse