Skip to content

Instantly share code, notes, and snippets.

@aldolat
Last active August 29, 2015 14:27
Show Gist options
  • Save aldolat/931422b7f537db604322 to your computer and use it in GitHub Desktop.
Save aldolat/931422b7f537db604322 to your computer and use it in GitHub Desktop.
<?php
/* *** SITUATION ***
* We want to retrieve 3 posts and show the sticky posts on top.
* To make sticky posts work as expected, the 's' parameter must be not declared or should be NULL,
* otherwise the sticky posts won't appear on top of other posts.
*/
/* *** CASE 1 ***
* This code works as expected ('s' is not declared) and sticky posts appear on top:
*/
$params = array(
'post_type' => 'post',
'posts_per_page' => 3,
'ignore_sticky_posts' => false,
// 's' => '',
);
$my_query = new WP_Query( $params );
if ( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
<hr>
<?php
/* *** CASE 2 ***
* This code works as expected ('s' is NULL) and sticky posts appear on top:
*/
$params = array(
'post_type' => 'post',
'posts_per_page' => 3,
'ignore_sticky_posts' => false,
's' => NULL,
);
$my_query = new WP_Query( $params );
if ( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
<hr>
<?php
/* *** CASE 3 ***
* This code fails ('s' is declared and is empty) and sticky posts behave as normal posts:
*/
$params = array(
'post_type' => 'post',
'posts_per_page' => 3,
'ignore_sticky_posts' => false,
's' => '',
);
$my_query = new WP_Query( $params );
if ( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif;
/* SO THE QUESTION IS:
* Why couldn't the 's' parameter be empty ('s' = '')
* to make sticky posts work as expected?
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment