0

I'm having the weirdest error.

When trying to display a custom loop, I get the following error:

Fatal error
: Uncaught Error: Call to a member function have_posts() on array in /.../custom-page.php:42 Stack trace: #0 /.../wp-includes/template-loader.php(106): include() #1 /.../wp-blog-header.php(19): require_once('/data/sites/web...') #2 /.../index.php(17): require('/data/sites/web...') #3 {main} thrown in
/.../custom-page.php
on line
42

Which is so weird, because I've seen the error with 'call to a member function on null' or something else, but not on array, which is usually fine for this function.

The content of my custom-page.php has something like this:

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <div class="col-12">
            <h1><?php the_title(); ?></h1>
            
            <div class="grid row">
                
                <?php foreach( $programs as $program ) {
                    
                    $custom_query = get_posts( [
                        'post_type'=> 'artikel',
                        'post_status' => 'publish',
                        'posts_per_page' => 1,
                        'order' => 'DESC',
                        'orderby' => 'date',
                        'meta_query' => [
                            [ 'key' => 'layout', 'value' => 'synopsis' ],
                            [ 'key' => 'programs', 'value' => '"' . $program . '"', 'compare' => 'LIKE' ]
                            ]
                        ] );
                    
                    if( $custom_query->have_posts() ) {
                        
                        echo 'test';
                        
                    }
                    
                } ?>
                
            </div>
            
        </div>
    </div>
</div>

<?php get_footer(); ?>
2

Well, get_posts() returns an array, so you probably (made a typo and) meant to use new WP_Query there:

get_posts() returns an array of, by default, post objects (see WP_Post), and arrays do not have methods (functions in a PHP class), hence you can't use the "arrow" on an array variable, and so that explains the error "Call to a member function have_posts() on array".

$array = array( 1, 2, 3 );
$array->foo(); // error - $array is an array, not an object

class MyClass {
    public function foo() {
        echo 'it works';
    }
}
$object = new MyClass;
$object->foo(); // works - $object is an object (or a class instance) and the
                // method foo() exists and callable/public

And the have_posts() in question belongs in the WP_Query class, so you may want to do $custom_query = new WP_Query( [ <args> ] ) and not $custom_query = get_posts():

$custom_query = new WP_Query( [
    // your args here
] );

I hope that helps, and if you need further help with WP_Query, check out the documentation.

3
  • Thank you! I didn't mean to use new WP_Query, I probably just don't understand how get_posts()works. So the loop is not working on an array (which I thought it was), but on ... objects or? – Stef Jan 13 at 8:35
  • I hope the revised answer helps, but as for the get_posts() usage, you can find it here on the documentation page. – Sally CJ Jan 13 at 9:16
  • 1
    Thanks, that helped! – Stef Jan 13 at 9:25

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.