I’ve been reading this by @nacin and getting to grips with wp_query over query_posts that I used to use.
What I want is:
-
to put this in a template file
-
to query all posts of this category, in this case ‘3’
-
to display, if available, the first result on the page the latest sticky post
-
after the first sticky, if one is set, display the rest of the posts excluding that sticky if it was set
Problems I have seen are:
– if I do posts_per_page = 1 on the sticky loop, I can not do posts_per_page = -1 on the rest of the posts loop. To workaround this I’ve just set the number to 999.
I should say now that the code I have works. However this is for a very high traffic site, and I want to make sure this is the best way of doing it, and I’m not sure I’m using wp_query right to do this since the original query’s are essentially the same as one another just with and without sticky posts.
global $wp_query;
$wp_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 1,
'category__in' => 3,
'post__in' => get_option( 'sticky_posts' )
));
while ($wp_query->have_posts()) : $wp_query->the_post();
$exclude_featured = $post->ID;
echo the_title();
echo '<br />';
endwhile;
echo '<br />';
echo '<br />';
global $wp_query;
$args = array_merge(
$wp_query->query_vars,
array(
'post__in' => null,
'posts_per_page' => 999,
'ignore_sticky_posts' => 1,
'post__not_in' => array($exclude_featured)
)
);
query_posts( $args );
while ($wp_query->have_posts()) : $wp_query->the_post();
if ( $exclude_featured == get_the_ID() )
continue;
echo the_title();
echo '<br />';
endwhile;
Thanks for any help guys.
you could use
wp_list_pluck();
Here is the really simple way to do it.
global $wp_query;
especially 2x.br
tags…several times if your using the<?php post_class();
Use CSS, that way you will automatically get a class called.sticky
!Here is an update using 2 queries to address the details I missed, since it will be somewhat safe.
I see 2 major problems. a) You shouldn’t modify a global variable directly & b) You shouldn’t use
query_posts
. Here’s a reworked examplefunctions.php
in the template file