0

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);



        }
}
  • I see you specified 'suppress_filters' => true,, this has a major performance penalty and should be avoided, and also prevents other plugins and optimisations from being able to work with that query – Tom J Nowell Oct 31 at 15:11
  • @TomJNowell that was actually a last attempt no get it to work and I'll remove it but it still isn't working for me with or without it. – CI_Guy Oct 31 at 15:14
0

I hate to tell you this, but you have a typo that's causing the problem. 'order '=>'asc', should be 'order' => 'asc', (Notice the space inside the 'order ' string.)

If that's not the issue, I'd to a string search on all the files in wp-content for pre_get_posts to see if the theme or a plugin is altering the query.

  • no, you're right. I realized this later but never udpated post. Thank you anyway. I'll select this as the answer. – CI_Guy Nov 1 at 1:21

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.