Forums

[resolved] [closed] Sticky Code to show Stick post first is not working (5 posts)

  1. fas.khan
    Member
    Posted 2 years ago #

    Hi all,

    I wrote this code to show Sticky Post first (if there is any) and otherwise, just show the post, which is written latest.

    I have to just show one post on the page (either sticky or latest). To achieve it, I wrote the following code:-

    The problem is that It is not showing any sticky post. It does show me latest post properly but is not picking the sticky post.

    I would be very thankful, if you can pick the problem or if you want, you can propose a better code! ...

    <?php
         $recentPosts = new WP_Query();
         $recentPosts->query("showposts=1&category_name='ewp-news'&orderby=date&array('post__in'=>get_option('sticky_posts'))");
    
         $stickyCheck = false;
         while ($recentPosts->have_posts()) : $recentPosts->the_post(); 
    
            // The first one is always a  Sticky, so if there is a sticky, it will check it and let it pass!
    
            if (!($stickyCheck))
    	{
    ?>
    
                   <?php
    
                        // Title and Excerpt of the Post.
    
                    ?>
    
                <?php
    	}
    
            //if this post was a sticky post! then make it true and dont let another one show itself!
    
            if (is_sticky())
    	   $stickyCheck = true;
    
            ?>
          <?php endwhile; ?>

    I will be very thankful to you, if you can help me in this.

    Many Thanks!

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    I think you might need to add caller_get_posts

    See here, information still applies.
    http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters

  3. fas.khan
    Member
    Posted 2 years ago #

    Thanks a lot for your reply !

    now it does show me a post when there is one with a sticky.

    But when there is no sticky, it doesn't show me anything. I wanted that if there is no sticky, it just shows me the latest post...

    Is it possible?

    Many Thanks.

  4. fas.khan
    Member
    Posted 2 years ago #

    I solved with the following code.

    $recentPosts = new WP_Query();
    
    	  	$sticky = get_option('sticky_posts');
    
    		$args = array(
    			'showposts' => 1,
    			'post__in'  => $sticky,
    			'caller_get_posts' => 1,
    			'orderby' => 'date',
    			'category_name' => 'ewp-news'
    		);
    
    		$recentPosts->query($args);
    
    		 while ($recentPosts->have_posts()) : $recentPosts->the_post(); 
    
    	    // Printing Title and Excerpt!
    
          <?php endwhile; ?>

    Many Thanks.

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    Glad you got it sorted.. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic