0

I have tow WP_Query loops running on a page. The first query displays the sticky posts and the second query is set according to meta_value. The trouble I am having is when there is no-sticky posts, the query seems to display all the available posts, which is definitely no what I need. I need it to display nothing found, if there are no posts found according to query.

 $com_soon = array( 
                'post_type' => 'post',
                'post__in' => get_option( 'sticky_posts'),
                'numberposts' => -1,
                'cat' => 2
                );

                $cs_profiles = new WP_Query($com_soon);
                if ($cs_profiles->have_posts()) :
                while ($cs_profiles->have_posts()) : $cs_profiles->the_post();?>
                //the loop here 
                endwhile; else:
                echo 'NOTHING FOUND';
                endif;  

I guess this is some wp fallback mechanism. Is there any way I could avoid this because the if-have-posts doesnot seem to work here.

2
  • Never mind, found a solution here at codex. For those who may stumble on the same issue. – gurung Jun 1 '14 at 21:54
  • 1
    It would be great if you can post your proper solution as an answer. You can also score yourself some reputation here – Pieter Goosen Jun 2 '14 at 4:22
0

The snippet below from the codex suggest that it would just display the first sticky post. If there are none, it wont return anything. Whatever your use case is please apply the logic accordingly.

$sticky = get_option( 'sticky_posts' );
$args = array(
    'posts_per_page' => 1,
    'post__in'  => $sticky,
    'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( isset($sticky[0]) ) {
    // insert here your stuff...
}

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.