php - Wordpress Custom Function in Plugin to Return Post ID from Specified Category -


hoping can me figure out why can't get_first_post_in_category return usable post id. @ moment using wordpress plugin boilerplate create plugin removes first post of specified category (through admin options page) primary loop. until try , programmatically retrieve post id.

here define hook (works fine):

// in private function define_public_hooks() $this->loader->add_action( 'pre_get_posts', $plugin_public, 'exclude_featured_post_pre_injection'); 

here callback function using exclude specific post main query. if $first_post_in_category_id manually set, works perfectly. if try , set " = get_first_post_in_category();" thrown 500 error.

public function exclude_featured_post_pre_injection($query){      // doesn't work     $first_post_in_category_id = $this->get_first_post_in_category();      //works     // $first_post_in_category_id = '427';      if ($query->is_home() && $query->is_main_query()) {         $query->set('post__not_in', array($first_post_in_category_id));     } } 

and here issue ::: function attempting return id of first post in category used in other action/filter callbacks. if call this, returns 500 error.

public function get_first_post_in_category(){      $cat_id = get_option('sticky_content_category_id');     // returns (string) '3' has posts in      $args = array(         'posts_per_page' => 1,         'cat' => $cat_id     );      $latest_cat_post = new wp_query($args);      while ( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();      $first_post_id = the_id();    endwhile;   wp_reset_postdata();    return $first_post_id; } 

any idea on best way resolve issues running into?

i cant see how can cause error 500 code below works me.

$category_id = get_cat_id('wordpress');  //echo $category_id;  $args = array(     'posts_per_page' => 1,     'cat' => $category_id,     'orderby' => 'id',     'order' => 'desc' );  $query = new wp_query($args);  while($query->have_posts()):$query->the_post();     echo the_id(); endwhile; 

Comments