I have a custom post type called projects, and its archive is at /projects
, on the archive page I get notices Notice: Trying to get property of non-object
every time I try to access the $post
i.e. $post->post_name
. Is this intended behaviour? Should I just accept that and always check the availability of $post
before using it?
if (!empty($post)) {
// Do something with $post
}
$post
within the loop.if ( have_posts() ) $post = $wp_query->posts[0];
$post
outside the loop in a post archive, mainly because it yields the problem of "which post?" There are other functions that can provide more useful information though, such as the current post type and its labels, or the current category/tag/term