admin管理员组文章数量:1026740
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
|
1 Answer
Reset to default -1You 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;
}
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
1 Answer
Reset to default -1You 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;
}
本文标签: phpHow to include support for all page typescalendar urlsarchiveetc
版权声明:本文标题:php - How to include support for all page types, calendar urls, archive, etc 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1741391002a1866338.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if ( $post_object )
before doing anything with it. – Jacob Peattie Commented Aug 13, 2021 at 12:47