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(); ?>
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(); ?>
Share
Improve this question
asked Jan 13, 2021 at 8:24
StefStef
762 silver badges10 bronze badges
1 Answer
Reset to default 2Well, 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.