I have a problem with showing sticky posts in a custom loop. This is the code I'm using to for the custom loop:

    $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(); 
    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' ); 

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?

  • Do you have sticky posts enabled in the theme? Aug 2, 2012 at 15:19
  • I'm using twentyeleven so yes they are enabled. Aug 2, 2012 at 15:28
  • In what way is it not working? Do you get any output. Do you get any errors with WP_Debug on?
    – mrwweb
    Aug 2, 2012 at 15:32
  • The post that is set as sticky in dashboard doesn't stick on the first page, it's getting replaced by the latest normal post. Aug 2, 2012 at 15:35

2 Answers 2


if you use 'post__in' => get_option('sticky_posts') then you are only filtering on posts that are sticky.

As mrwweb said you need to look at your query. Then think of the 'loop' you want.

Do you want to show all posts in a specific query or all posts that are sticky in all categories?

  1. You might need to do 2 queries or a customer database query to select the posts you want.

  2. You are better off adding a tag or custom meta data to a post as you can't create a loop for sticky posts in a specific category.

  • I want to show the latest 2 posts from category 'events' and if one post from that category is sticky, I want it to be shown first. I don't get it why is this so hard to accomplish on wordpress... Aug 4, 2012 at 22:27
  • You should use a Tag or Custom Meta Data to create a custom sticky post. Fetch Sticky posts by Category is not possible.
    – Damien
    Aug 5, 2012 at 8:39

Some background

You're trying to use query_posts AND WP_Query at the same time. You only use one at a time and they're used in different ways. You'll need to review the WP_Query and query_posts pages on the Codex and then also read these two WPSE threads:

Now, you'll see that on the WP_Query page that sticky posts are enabled by default, so this really should cause you any problems. The query_posts line you have is straight off the Codex and with this description:

Display just the first sticky post:

Fixing issue

Your WP_Query stuff looks ok, so I suspect the problem is somewhere in your loop or with the content itself.

  1. Remove the query_posts stuff that you don't need.
  2. Double check that there is a post in the category you want that is sticky.
    • Do a var_dump() of get_option( 'sticky_posts' ) and confirm that the ID of the page you want is there.
  3. Try adding 'ignore_sticky_posts' => 0 to your WP_Query $args.
  4. Make sure you haven't hardcoded a different loop on content-home.php. It's possible that you're outputting a different loop on that page.
  • Thanks for the detailed explanations! I removed the query_posts, did a var_dump() and got a valid output. I've also added the 'ignore_sticky_posts' argument but it's still not working. I've even removed the get_template part and tried it with the_title() and the_content() but it was the same output. I've also included in the main post two pastebin links to the homepage.php and content-events.php. Aug 2, 2012 at 16:12
  • Looking at your home.php, I see that you keep saving new WP_Query instances to $post_from_cat_a. I don't know if that's causing the problem, but you might be running into some weird stuff there. Save each new instance of the WP_Query class to a new variable and see if that resolves it.
    – mrwweb
    Aug 2, 2012 at 17:31
  • I've changed them to a,b,c but it's the same. Could it be something wrong with the whole wp setup? I'll have to try the same code on a different machine. Aug 2, 2012 at 19:38
  • 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. Aug 4, 2012 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.