I try to list pages of custom post type in archive.php, and I get this error message:
Invalid argument supplied for foreach()
$post_type = get_post_type( get_the_ID() );
$pages = get_pages(array( 'post_type' => $post_type ) );
foreach ($pages as $page ) {
echo '<div class = "prop-sidebar-section">';
echo $page->post_title;
echo '</div>';
}
the weird thing is, that with get_pages(array( 'post_type' => 'page'); is working, and if I use get_pages(array( 'post_type' => 'post') ); is not.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
get_pages() is intended only for hierarchical post types like page. For non-hierarchical post types like post, get_pages() will return a false, hence that explains why get_pages( array( 'post_type' => 'post' ) ) “doesn’t work”, i.e. the returned value is not an array, so it can’t be used with foreach:
// Good: 'page' is a hierarchical post type.
$pages = get_pages( array( 'post_type' => 'page' ) );
// Output: $pages is an array. Can do foreach ( $pages ... )
// Not good: 'post' is a non-hierarchical post type.
$posts = get_pages( array( 'post_type' => 'post' ) );
// Output: $posts is a FALSE. Can't do foreach ( $posts ... )
// because foreach ( false ... ) is invalid.
So for non-hierarchical post types, you would use get_posts() or make a new instance of WP_Query (i.e. new WP_Query()):
$posts = get_posts( array( 'post_type' => 'post' ) );
// or..
$query = new WP_Query( array( 'post_type' => 'post' ) );
$posts = $query->posts;
Method 2
I am assuming your code is not within a wordpress loop,
something like while(have_posts()): the_post();
which means get_the_ID will return null,
try var_dump(get_the_ID); if it is null thats why your code is not working.
and to get your post type you can use this:
var_dump($wp_query->query,get_queried_object()); die;
$wp_query->query will have post_type component for custom post types. That will not be there for post post types. get_queried_object will return quite a bit of data for custom post types but null for post post type.
Method 3
Run this code and will return all you got, then you can check if there your $post_type is returning ‘page’ or ‘your_custom_post_type’, and also check all available with the function get_pages();
$post_type = get_post_type( get_the_ID() );
echo '<pre>';
print_r($post_type);
echo '<br> ####################################### <br>';
$pages = get_pages(array( 'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,) );
echo '<pre>';
print_r($pages);
In my case I only have $post_type returning ‘page’ and $pages have a lot of info but all post_types are ‘page’. Hope that can help some how!..
And a few minutes later after reading Sally CJ answer, I had use the get_posts instead and works like a charm.
$post_type = get_post_type( get_the_ID() );
$pages = get_posts( array( 'post_type' => $post_type ) );
foreach ($pages as $page ) {
echo '<div class = "prop-sidebar-section">';
echo $page->post_title;
echo '</div>';
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0