• 000000000

    (@pealo86)


    I have a WP_Query object that is trying to query sticky posts. I have one after the other in fact, one queries stickies and the second queries none-stickies.

    <?php
    
    	/** Grab the sticky post ID's */
    	$sticky = get_option('sticky_posts');
    
    	/** Query the sticky posts */
    	$args = array(
    	    'post__in'          => $sticky,
    	    'posts_per_page'    => 3,
    	    'cat'         		=> 210
    	);
    	$sq = new WP_Query($args);
    
    	/** Count the number of post returned by this query */
    	$sq_count = $sq->post_count;
    
    	/** Output your sticky posts */
    	get_template_part('loop', 'feed-videos-sticky' );
    
    	/** Check to see if any non-sticky posts need to be output */
    	if($sq_count < 3) :
    
    	    $num_posts = 3 - $sq_count;
    
    	    /** Query the non-sticky posts */
    	    $sticky = get_option('sticky_posts');
    	    $args = array(
    	        'post__not_in'      => $sticky,
    	        'posts_per_page'    => $num_posts,
    	        'cat'         		=> 210
    	    );
    
    	    $nq = new WP_Query($args);
    
    	    /** Output your non-sticky posts */
    	    get_template_part('loop', 'feed-videos-sticky-none' );
    
    	endif;
    
    ?>

    And inside my (first) loop file I have the following:

    <?php global $sq; ?>
    
    <?php if ( $sq->have_posts() ) : ?>
    
    	<?php while ( $sq->have_posts() ) : ?>
    
    		<div class="grid_4 video">
    
    			<a href="<?php the_field('video'); ?>" data-fancybox-type="iframe">
    
    				<span class="title">
    					<h5><?php the_title(); ?></h5>
    				</span>
    				<span class="play"></span>
    
    				<img src="<?php echo getPostThumbnailUrl('video'); ?>" alt="<?php the_title(); ?>" />
    			</a>
    		</div>
    
    	<?php endwhile; ?>
    
    <?php endif; ?>
    
    <?php wp_reset_postdata(); ?>

    But I keep getting this error on the frontend:

    Fatal error: Call to a member function have_posts() on a non-object in /Library/Server/Web/Data/domains/…/loop-feed-videos-sticky.php on line 3

    If I echo gettype($sq); it gives me a value of NULL

    I’ve globalised the variable however, so I’m not sure what else I can do? Could there be some problems with the arguments in my query?

  • The topic ‘Call to a member function have_posts() on a non-object’ is closed to new replies.