Custom query: how to get the sticky posts first?

Further upgrade: just built a class to place in functions.php or maybe in a plugin. I did that mainly because I want almost always have sticky posts, probably in more loops per page. So got a lot of duplication. Therefore here is a basic class, that certainly can be exapanded/perfectioned some way, but for my … Read more

Loop for sticky posts

The issue is that you are using the function query_posts this only queries the default result on that page. It’s advised that you use wp_query instead, it just eliminates the margin for error or unexpected results. You can create a new query like below, and specify explicitly how many posts to return: <?php $sticky = … Read more

Exclude first post (sticky or not) from the loop using query_posts()

You shouldn’t really need to “exclude” anything. Just make use of the built in loop tracking. WP_Query will “remember” where you left off. For example: if (have_posts()) { while (have_posts()) { the_post(); echo ‘firstpost##’; the_content(); echo ‘##endfirstpost’; break; } } // do some other stuff if (have_posts()) { while (have_posts()) { the_post(); echo ‘otherposts##’; the_content(); … Read more

Make posts non-sticky

You can use a simple script which you run once and remove. The idea is to replace the sticky post array with an array of the 4 desired ID’s PHP 5.4+ version – short array syntax add_action( ‘init’, function () { // Define our new array $stickies = [1,2,3,4]; // Change to match your own … Read more

How to modify query on category pages to show only sticky posts?

Just set ‘post__in’ to a null array. add_action( ‘pre_get_posts’, function( \WP_Query $q ) { if( ! is_admin() && $q->is_category() && $q->is_main_query() ) { $sticky_posts = get_option( ‘sticky_posts’ ); //If there are sticky posts if( ! empty( $sticky_posts ) ) { $q->set( ‘post__in’, (array) $sticky_posts ); } //If not else { $q->set( ‘post__in’, array(0) ); } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)