WP_Query for liked posts is showing all posts if none are liked

I'm using the plugin Favorites for WordPress to allow visitor's to like products presented with a Masonry grid on the site. On the site I have a page which lets the visitor see all his or hers liked products and it loads them 20 per page with AJAX.

I used This guide for my WP_Query and everything works fine as long as the visitor has liked posts, but if there are no liked posts - the page is showing all posts from the site instead of triggering the else-statement for $custom_query->have_posts();

Any suggestions? The code:

? get_header(); ?
?
$likedposts = get_user_favorites($user_id = null, $site_id = null, $filters = null);

// Define custom query parameters
$custom_query_args = array( 
    'orderby' = 'DATE',
    'post__in' = $likedposts,
    'posts_per_page' = 20
);

// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );

// Pagination fix
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

if ($custom_query-have_posts()) :?
    div id="primary" class="content-area"
        main id="main" class="site-main loop-content" role="main"
            ul class="grid grid-all" id="grid-index"
                ? while ($custom_query-have_posts()) : 
                    $custom_query-the_post();
                    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post-ID), 'thumbnail_size' );
                    $url = $thumb['0']; ?

                    li class="product-item"
                        div class="pi-heart"
                            ? echo do_shortcode('[favorite_button post_id="" site_id=""]'); ?
                        /div
                        a href="?php the_permalink();?" data-fancybox-type="ajax" class="various fancybox.ajax lighten" id="?php the_ID(); ?" data-class="?php echo get_post_type( get_the_ID() ); ?"?php the_post_thumbnail('thumbnail'); ?/a
                    /li
                ?php endwhile; ?
            /ul
        /main!-- #main --
        /div!-- #primary --
? else: ?
    p?php _e( 'Sorry, no posts matched your criteria.' ); ?/p
? endif;
wp_reset_postdata(); ?

div id="nextnav" style="display: none; background-color: white; border: 3px solid black; z-index: 99; text-align: center; width: 200px;"
    ? 
        next_posts_link($custom_query-max_num_pages);
        // Reset main query object
        $wp_query = NULL;
        $wp_query = $temp_query;
    ?
/div

Topic masonry ajax wp-query Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.