0

I am trying to display a sticky post by its category id. The sticky post is displayed, but the problem is that the published sticky post is displayed twice.

You can see from the snap shot below that the same sticky post is displayed twice (it is highlighted with the blue arrow). I don't want to display the sticky title news in the sub tittle of sticky news.

You can see in bellow snap. These are same news the first arrow is sticky I have set from backend and second arrow is the same news displaying bellow the sticky see here I don’t want display second arrow news its twice here I wanted to display another news except the second arrow News.

sticky

Bellow is the code to set sticky from widget post

<ul>
<?php 

$args = array(
            'category_name' => 'maharashtra',
            'post__in' => get_option( 'sticky_posts' ),
            'showposts' => '1',

        );

$the_query = new WP_Query( $args );

    while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
  <li>      <div class="thumbnail_class"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_post_thumbnail(array(342,173), array ('class' => 'alignleft1')); ?></a></div>
        <div class="link_contect"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div><li>
      <?php 
    endwhile;

?>
</ul>

Bellow is the my sample code to display the recent post.

<div class="home-post home-post-first green">
<h2><a href="http://majhapaper.com/test/विभाग/महाराष्ट्र/">महाराष्ट्र</a></h2>
<?php 
$recent = new WP_Query("cat=255&showposts=6&ignore_sticky_posts=1"); 

?>

<ul>
<?php

    while($recent->have_posts()) : $recent->the_post(); 
$postvariable++;

if ($postvariable == 1) {
 ?>
<li>[widget id="text-8"]</li>
<?php  
} else { ?>
<li>            
            <div class="link_contect"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>

            </div> 
  • I think we are going in circles here. I might have misunderstood you, in your pic, the first title inside the green area just below your pic, you want that title gone? – Pieter Goosen May 27 '14 at 12:28
  • Yes you are right I want it display one time on the up side as sticky news but it display 2time pointed with blue arrow are the same news I want it only one on up side like pic – Sanjay Nakate May 27 '14 at 14:07
  • 1
    showposts is depredciated, you should be using posts_per_page – Pieter Goosen May 28 '14 at 6:57
  • Are category_name 'maharashtra' and cat ID 255 the same category, or are these diffirent categories all together. Sorry for asking so much questions, but I'm still in the dark a bit. One more thing, what is [widget id="text-8"] – Pieter Goosen May 28 '14 at 7:26
  • [widget id="text-8"] is shord code to display sticky from widget – Sanjay Nakate May 28 '14 at 7:59
5

If you want to totally remove the sticky posts from the query, you need to use post__not_in. The Codex has an example, which you can adapt to your needs:

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
    'cat' => 3,
    'ignore_sticky_posts' => 1,
    'post__not_in' => $sticky,
    'paged' => $paged
);
$query = new WP_Query( $args );

http://codex.wordpress.org/Sticky_Posts

ignore_sticky_posts doesn't remove sticky posts from the query; it just ignores the fact that they are sticky, and lists them in their natural position.

  • if i used the post__not_in insted of post__in i am not able set stcky from the widget – Sanjay Nakate May 28 '14 at 6:22
  • I'm referring to the second loop, not the first. You can set post__in for the first loop (your sticky post) and post__not_in (replacing ignore_sticky_posts) for the second. – markcbain May 28 '14 at 6:50
  • your code is working but i am not able to display my first post bellow the stick otherwise code is good – Sanjay Nakate May 28 '14 at 10:55
  • i am using this code code is working but having one issue its not displaying the first post it displaying from second post that the issue other wise your answer is good.see the bellow code which i have used – Sanjay Nakate May 28 '14 at 11:50
  • <ul> <?php $sticky = get_option( 'sticky_posts' ); $args = array( 'cat' => 255, 'ignore_sticky_posts' => 1, 'post__not_in' => $sticky, showposts=>6 ); $recent= new WP_Query( $args ); while($recent->have_posts()) : $recent->the_post(); $postvariable++; if ($postvariable == 1) { ?> <li>[widget id="text-8"]</li> <?php } else { ?> <li> <div class="link_contect"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a> </div> </li> <?php } ?> <?php endwhile; ?> </ul> – Sanjay Nakate May 28 '14 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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