1

I'm creating a theme in which I've created a page template called page_default-blog.php to be used for creating a blog page. In this file I have the following code:

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
  'paged' => $paged,
);
$custom_query = new WP_Query($args);

if ($custom_query->have_posts()) : 
  while($custom_query->have_posts()) : $custom_query->the_post(); ?>
    the_title();
    echo "<br>";
  endwhile;
endif;
wp_reset_postdata();

The problem is that the sticky post doesn't come up at the top, even though the ignore_sticky_posts is set to false by default in $args.

The sticky post is showing at the top if I use the 'index.php' but it's not coming if I set the front page as the blog page.

Please let me know how I can get this to work. Thanks.

Edit 1: Following is the result of var_dump ($custom_query->request); :

string(217) "SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC LIMIT 0, 4"

Edit 2: This is what my template file page_default-blog.php looks like:

<?php
/*
    Template Name: Blog Default
*/
get_header();

wp_reset_postdata();
wp_reset_query();

$args = array(
  'post_type' => 'post',
  'posts_per_page' => -1,
  'ignore_sticky_posts' => 0,
);
$custom_query = new WP_Query( $args );
var_dump( $custom_query->is_home ); // This is returning bool(false)
?>

<div class="container">
  <?php
  if ($custom_query->have_posts()) : while($custom_query->have_posts()) : $custom_query->the_post(); ?>
    <?php
    echo the_title();
    echo "<br>";
    ?>
    <?php endwhile;
  endif;
    wp_reset_postdata();
  ?>
</div>

<?php get_footer(); ?>

The above is the exact file that I'm using right now. I simplified it to try to find the problem, but still haven't been able to, as the title for the sticky post is not appearing at the top of the list.

12
  • Where is ignore_sticky_posts set to false in the above $args ? Commented Jun 7, 2015 at 8:39
  • the defaults, if any, would be for a page, not an index list; try directly setting the sticky post parameters in your query.
    – Michael
    Commented Jun 7, 2015 at 11:56
  • Sorry, I meant that it is by default set to false and you only set it to true if you don't want the sticky post to show. Also, I tried by setting that to false in the $args array, but still it's not working. Commented Jun 7, 2015 at 12:11
  • Add the results of ` var_dump ($custom_query->request);` to the question please.
    – s_ha_dum
    Commented Jun 7, 2015 at 12:46
  • @s_ha_dum I've updated the question with the result. Thanks. Commented Jun 7, 2015 at 12:53

1 Answer 1

4

You have custom filter in a plugin or in your theme that is messing with WP_Query. Your issue is that something is setting is_home to false. This is not default behavior, by default, is_home should return true in any custom instance of WP_Query.

is_home is one of the contributing factors which is responsible for setting up sticky posts at the top in a query, and if its value is false, you will not get sticky posts moved to the front.

You should deactivate your plugins one by one and check which one is causing the issue. You should also test your code on a bundled theme to eliminate your theme as a possible culprit

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.