I have a problem with showing sticky posts in a custom loop. This is the code I'm using to for the custom loop:
<?php
$post_from_cat_a = new WP_Query(array(
'category_name' => 'events', //Get posts from category a
'posts_per_page'=> 2 //Limit it to the latest one
));
if( $post_from_cat_a->have_posts() ){
while( $post_from_cat_a->have_posts() ): $post_from_cat_a->the_post(); //Display output here for post from category a
$category = get_the_category();
if($category[0]){
echo '<div class="cat-events"><a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a></div>';
}
get_template_part( 'content', 'events' );
endwhile;
}
?>
I can't figure it out why it doesn't work.
Later edit: this is my entire homepage.php where the problem occurs and this is the content-events.php file used for each post in the events category.
Second edit: If I include 'post__in' => get_option('sticky_posts')
in the wp_query
array the sticky post is shown but it doesn't list the next post in the category. Only the sticky posts appears on the page, it's kind of ignoring the 'posts_per_page'=> 2
argument.
Does anyone have any idea why this is happening?