wordpress - Why is $wp_query->post unset during 404? -
i using plugin called connections. when 404 occurs (for reason, i.e. page doesn't exist accessed via browser) custom post callback filter, specified in plugin's code breaks, $wp_query->post
unset. complaint trying property of non-object when $wp_query->post->id
accessed. expect.
i have disabled other plugins , gone twenty fifteen theme issue still occurs. on clean install problem doesn't occur. doesn't make sense me. if it's not plugin or theme issue , it's not in wordpress core issue? corrupt database? (everything else seems working fine).
the filter callback here:
public static function filterposttitle( $title, $id = 0 ) { global $wp_query, $post, $connections; // added: if add line problem goes away. if ( is_404() ) return $title; // whether or not filter page title current directory location. if ( ! cnsettingsapi::get( 'connections', 'connections_seo', 'page_title' ) ) return $title; // if uncomment next 2 lines , comment out following line of code, problem goes away. //$post_id = get_queried_object_id(); //if ( ! is_object( $post ) || $post_id != $id || ! self::$filterpermalink ) return $title; if ( ! is_object( $post ) || $wp_query->post->id != $id || ! self::$filterpermalink ) return $title;
if add following check @ start of function problem goes away (again we'd expect):
if ( ! isset( $wp_query->post ) || ! isset( $wp_query->post->id ) ) return $title;
i have printed out backtraces give me clues, backtraces don't seem give many clues global variable $wp_query @ fault. reminder why globals bad practise... poor show wordpress. globals make debugging nightmare. global variable changed? don't know.
the filter callback set follows:
add_filter( 'the_title', array( __class__, 'filterposttitle' ), 10, 2 );
the developer of plugin (maybe rightly so) thinks problem lies outside of plugin.
question: why $wp_query->post unset @ time of callback being triggered? should $wp_query->post set time 'the_title' filter callback triggered?
as may guess new wordpress. it's fantastic... not fan of global variables though... big no-no.
it's not $wp_query->post
unset during 404...but rather it's never set @ all. in fact, $wp_query->post
unset new instances of wp_query
, when initialized. let's look @ source:
/** * initiates object properties , sets default values. * * @since 1.5.0 * @access public */ public function init() { unset($this->posts); unset($this->query); $this->query_vars = array(); unset($this->queried_object); unset($this->queried_object_id); $this->post_count = 0; $this->current_post = -1; $this->in_the_loop = false; unset( $this->request ); unset( $this->post ); // unset unset( $this->comments ); unset( $this->comment ); $this->comment_count = 0; $this->current_comment = -1; $this->found_posts = 0; $this->max_num_pages = 0; $this->max_num_comment_pages = 0; $this->init_query_flags(); }
next, query parsed, , if there's error:
if ( '404' == $qv['error'] ) $this->set_404();
the 404 status set, , $wp_query->post
never populated/set.
Comments
Post a Comment