Custom WP_Query with no posts to output

I have created a custom WP_Query as seen below. The query works well except for when $favorites is empty. When $favorites is empty, the query still outputs posts, which I don´t want. It could seem like it defaults back to the standard WP_Query to output all published posts.

Anyone knows what´s going on?

Read More
$favorites = wpfp_get_users_favorites($user = '');
$favorites_query = new WP_Query(array('post__in' => $favorites));

while ($favorites_query->have_posts() ) : $favorites_query->the_post();

Thanks!

Related posts

Leave a Reply

1 comment

  1. Empty argument here does not equal logical in nothing. It equals no value to process and so WP ignores it and goes further as usual.

    Just wrap your query in if( !empty( $favorites) ) condition.