Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've tried everything. I've looked at every. single. question. here, on StackOverflow, the WP help forum, googled 10 pages deep and literally tried EVERY combination of code I could find, for the last 2 days, and can't get anything to work how I want it. Surely it can't be impossible? The goal seems so simple!

THE GOAL: Show ALL sticky posts first, and then normal posts after them - WITH PAGINATION.

Example: With posts per page set to 10, having 15 sticky posts and 15 normal posts, page 1 would be 10 sticky posts, page 2 would be 5 sticky posts and then 5 normal posts, and page 3 would be 10 normal posts. Ordered by date.

I've tried multiple loops, various queries, and have come CLOSE but so far no cigar. Here's what I have so far:

<!-- THIS CODE QUERIES ALL POSTS AND RETURNS ONLY STICKY POSTS, DISPLAYED AT TOP OF THE PAGE -->
<?php
// show only ads within this specific category
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
    'post_type' => 'my_custom_post_type',
    'ad_cat' => $term->slug,
    'caller_get_posts' => 1,
    'post__in' => $sticky,
    'posts_per_page' => -1
    //'paged' => $paged
    );
query_posts($args);
?>
<?php get_template_part( 'loop', 'post_featured' ); ?>
<?php wp_reset_query(); ?>

<!-- THIS CODE QUERIES ALL POSTS AND RETURNS ONLY REGULAR POSTS, DISPLAYED BELOW THE STICKIES -->    
<?php
// show only ads within this specific category
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$sticky=get_option('sticky_posts');
$args=array(
    'post_type' => 'my_custom_post_type',
    'ad_cat' => $term->slug,
    'caller_get_posts' => 1,
    'post__not_in' => $sticky,
    'paged' => $paged,
    );
query_posts($args);
?>
<?php get_template_part( 'loop', 'post_normal' ); ?>
<?php wp_reset_query(); ?>

The above shows ALL sticky posts and then (5) normal posts. This is the closest I have gotten, but #1, I don't want to show ALL sticky posts on the first page, I want to adhere to the pagination rules. #2, ALL sticky posts show up on all paged pages. So if I have 50 sticky posts and 50 normal posts, each page shows 55 posts - the first 50 are the sticky posts and the last 5 are normal, which is not my goal (see THE GOAL above).

Is there an easy modification to one or both loops? Should I be using a double loop or is there another/better option to do this? I'm open to suggestions as long as I get it to WORK. I should note: I'm using WP 3.2.1 and a slimmed down non-plugin pagination function which was taken from WP-PageNavi (full pagination function can be seen here: http://paste2.org/p/1596821).

Any help is appreciated!

share|improve this question
    
Your code sample queries custom post types. CPTs don't have the sticky option. –  Chris_O Aug 19 '11 at 23:42
    
Well that's funny because they work in every other loop I'm using? –  RodeoRamsey Aug 19 '11 at 23:55
    
How did you even make the Custom Post Type Sticky? There is no check box for it on the editor? –  Chris_O Aug 19 '11 at 23:59
    
It must be in the theme I'm using... yes, I am pretty sure. There is a front-end form for adding a post with some custom stuff, and one of them is a checkbox to make the post sticky. Either way, the sticky option IS applied, so now I have to figure out my initial goal. :) Any ideas on that? –  RodeoRamsey Aug 20 '11 at 0:07
    
As an aside, when the CPT is initialized in the theme functions, 'sticky' is included in the 'supports' array, so wouldn't that mean that CPTs can have the sticky option? –  RodeoRamsey Aug 20 '11 at 0:13
show 4 more comments

2 Answers

up vote 0 down vote accepted
+50

@Chris_O is right... currently sticky posts are not supported in custom post types so it must have something to do with the theme you are using.

Now disregarding the CPT problem... and assuming that it does work the same way wordpress stickies do then the goal that you are describing is exactly the way 'sticky' posts are designed to work in wordpress (i.e. show all the sticky posts before showing the normal posts and adhere to pagination), so from what I can tell your problem lies in the fact that you are trying to create two separate loops. In order to display the posts they way you want them to be displayed you should be using only one loop.

Have you tried just running one loop (i.e. removing the second query and replacing the first one with something like the below to see if the stickies are automatically added)?

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$args=array(
  'post_type' => 'my_custom_post_type',
  'paged' => $paged,
  'posts_per_page' => 10
);
query_posts($args);

get_template_part( 'loop', 'post_normal' );
share|improve this answer
1  
It seems that the stickies are re-arranged to the first indexes of the post array, regardless of the post_per_page parameter. So if you want to display only 5 posts per page and you have 2 stickies, you will get 7 posts on the first page. I think this is RodeoRamsey's problem here. @RodeoRamsey Look at the sourcecode of the default behavior on the initial loop wp-includes/query.php at line 2681, maybe you find a solution to create your own sticky loop with this examples. –  rofflox Aug 27 '11 at 10:41
    
Thanks Roman - actually I ended up needing to do a combo of the above, but @drebabels was correct in a sense: it needed to be ONE loop - one loop that had 3 if/else statements. First, work out how many sticky posts are in the category. Then display only sticky posts get_template_part of "featured". Then, if the page has a combo of sticky and normal posts, split them out using query_posts and a custom array. Finally, if the page only had regular posts, use the get_template_part of "normal". It was a mess of code. Then the pagination had to have a tweak. I hired a developer. THANKS ALL. –  RodeoRamsey Aug 28 '11 at 14:58
add comment

I would combine your template loop with the code above.

And then insert some condition to the 1st loop and 2nd loop.

if($paged == 1) {
//Show Special Content for First

} else if($paged => 2) {
//Show Content for pages greater than 2

}

This probably would not work out of the box but it is definitely in the right direction by using more conditions to get the right settings you want.

Hence, I am a new user with this account on the wordpress exchange, I can't post comments and ask that you combine the code with the "get_template_part('loop')" or the loop template. I could probably give a better answer from there.

share|improve this answer
    
Thanks for the reply. Actually, that wouldn't work because some categories have so many posts that the "normal" ones don't fall until the 5th or 6th page. I suppose if it could be done programmatically somehow, it would work, but I ended up finding another solution. –  RodeoRamsey Aug 28 '11 at 14:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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