-4

I have been looking for a long time, but I could not find a solution. I'm not a coder. Please help me. Here is my wp_query code;

$my_query = new WP_Query('category_name=animals&showposts=10');
while ($my_query->have_posts()) : 
    $my_query->the_post();
    $do_not_duplicate = $post->ID; ?> 
    <li><a href="<?php the_permalink()?>"><?php the_title(); ?></a></li><?php 
endwhile; ?> 

2 Answers 2

2

It is not entirely clear what you are trying to do but sticky posts should be at the top-- that is, the first posts displayed-- already unless you have made an effort to prevent that. That is the default. I just tested this with your query, changing only the category ID to something that exists on my server.

To prevent this sticky post juggling-- that is to cause the opposite behavior-- you can pass a parameter to WP_Query like :

$my_query = new WP_Query('category_name=animals&showposts=10&ignore_sticky_posts=true');

Or set the value before the query runs with:

$my_query->set('ignore_sticky_posts',true); // $my_query could be any instantiated WP_Query variable.

Perhaps search your theme and plugins for ignore_sticky_posts and see if something is messing with the query.

You could also try to force the sticky posts juggling with:

function force_sticky_wpse_98187($qry) {
  $qry->set('ignore_sticky_posts',false);
}
add_action('pre_get_posts','force_sticky_wpse_98187',1000);

That adds a filter with an arbitrarily high priority so that it should reasonably run as the last action on that hook. I do not know if it works. It is completely untested. That also changes (if it works) all queries, which is probably too aggressive. You would want to add additional conditions to restrict when it runs.

1
$category = get_cat_ID('animals');

// The Query

$args = array('post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1, 'order' => 'ASC' , 'cat' => $category);
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
?>
    <div id="sticky_title"><h1><?php the_title();?></h1></div>
    <div id="sticky_excerpt"><p><?php the_excerpt(); ?></p></div>
<?php

endwhile;

// Reset Query

wp_reset_query();
4
  • thanks for answer but i dont get it. May 3, 2013 at 10:10
  • what u r getting
    – Balas
    May 3, 2013 at 10:32
  • i try this code its working but not very well. when i make a post to 'sticky' this code display only 'sticky' posts. so i want to display 10 post from 'animal' category but sticky posts are on the top May 3, 2013 at 11:43
  • 2
    @BOTH Please use proper formatting (WYSIWYG editor, "help" link for further info) upper- and lowercase characters. This is no chat and should be a reference for later readers. Thanks.
    – kaiser
    May 3, 2013 at 13:24

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.