最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to include support for all page types, calendar urls, archive, etc

programmeradmin3浏览0评论

I'm writing a plugin and I get this error when viewing calendar urls for example the calendar archive list on a wordpress site. I do not get the error on posts or pages, only get error when viewing a set of posts via calendar/archive/calendar url (example of what I mean by calendar url. thewebsite/2021/08 -not an actual link-.). (Also, the error shows on the search page.) I assume the error means I am not including compatibility for other types of pages, like archives, searches, urls showing sets by date, etc. I'm new to programming. Please give me any comments if you have any thoughts after seeing this portion of code. The error message is "Notice: Trying to get property 'post_type' of non-object" The code I am trying to write better:

$wp_the_query   = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();
$post_object    = sanitize_post( $queried_object );
$post_type      = $post_object->post_type;
$post_id        = $post_object->ID;

UPDATE: Below is the way I solved the problem, changed the code to this: (please comment if you see problems.)

$wp_the_query   = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();

if ( is_singular() ) :
 $post_object    = sanitize_post( $queried_object );
 $post_type      = $post_object->post_type;
 $post_id        = $post_object->ID;
endif;

I'm writing a plugin and I get this error when viewing calendar urls for example the calendar archive list on a wordpress site. I do not get the error on posts or pages, only get error when viewing a set of posts via calendar/archive/calendar url (example of what I mean by calendar url. thewebsite/2021/08 -not an actual link-.). (Also, the error shows on the search page.) I assume the error means I am not including compatibility for other types of pages, like archives, searches, urls showing sets by date, etc. I'm new to programming. Please give me any comments if you have any thoughts after seeing this portion of code. The error message is "Notice: Trying to get property 'post_type' of non-object" The code I am trying to write better:

$wp_the_query   = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();
$post_object    = sanitize_post( $queried_object );
$post_type      = $post_object->post_type;
$post_id        = $post_object->ID;

UPDATE: Below is the way I solved the problem, changed the code to this: (please comment if you see problems.)

$wp_the_query   = $GLOBALS['wp_the_query'];
$queried_object = $wp_the_query->get_queried_object();

if ( is_singular() ) :
 $post_object    = sanitize_post( $queried_object );
 $post_type      = $post_object->post_type;
 $post_id        = $post_object->ID;
endif;
Share Improve this question edited Aug 13, 2021 at 13:36 Angel Hess asked Aug 13, 2021 at 0:01 Angel HessAngel Hess 137 bronze badges 4
  • Not all queries have a queries object. So after using get_queried_object() you need to check if a value was returned. – Jacob Peattie Commented Aug 13, 2021 at 7:08
  • I don't understand how to do that exactly. But I will tell you all the visual aspects are correct in the page. But the error shows. So I assume you mean I need to make the function skip only part of the function. Correct? – Angel Hess Commented Aug 13, 2021 at 12:35
  • Right, just check if ( $post_object ) before doing anything with it. – Jacob Peattie Commented Aug 13, 2021 at 12:47
  • Thanks. That gave me ideas and I think I solve the problem. See the edited original if you want to see what I wrote to solve the issue. – Angel Hess Commented Aug 13, 2021 at 13:52
Add a comment  | 

1 Answer 1

Reset to default -1

You need to add a check if the view that you are currently on is a single-view page or an archive ( or any other taxonomy page ). If you are on an archive page, the get_queried_object() will not return a Post object, therefore the error

The check you need to add can be the following:

if ( ! is_archive() ) {
    $wp_the_query   = $GLOBALS['wp_the_query'];
    $queried_object = $wp_the_query->get_queried_object();
    $post_object    = sanitize_post( $queried_object );
    $post_type      = $post_object->post_type;
    $post_id        = $post_object->ID;
}
发布评论

评论列表(0)

  1. 暂无评论