plugin development – On wordpress.com, get_the_terms does not detect my custom query_var

Question

This bug happens only on wordpress.com installs but works as expected on local installs and non-wordpress.com instances. Their support was not able to help claiming they can’t assist with custom code so I have to reach out to the gurus here.

The intent of the code is this: when a page does not exist, I forward it to a hard-coded post (with ID 1) and change the title, content and categories with something custom.

This is the code. It adds a custom query_var which should typically be detected in all the hooks but it is detected only in the post hooks (post_title, post_content etc.) but not in other hooks such as get_the_terms. Again, it works properly on non-wordpress.com installs.

function is_atl_defined() {
    $var = get_query_var('_atl_');
    return empty( $var ) ? false : $var;
}

add_filter( 'query_vars', function ( $vars ) {
    $vars[] = '_atl_';
    return $vars;
}, 1, 1 );

add_action( 'template_redirect', function(){
    global $wp_query;

    // if the page is 404, set it to a hard coded page with id 1
    if ( is_404() ){
        $wp_query->is_404 = false;
        status_header(200);
        header("HTTP/1.1 200 OK");
        
        $wp_query->query("page_id=1&post_type=post&_atl_=test");
        $wp_query->the_post();
        $wp_query->is_single = true;
    }
}, 999 );

// should append with " atl is defined" and does
add_filter( 'the_title', function( $title ) {
    global $post;

    if ( is_atl_defined() ) {
        $title = $title . " atl is defined";
    }
    return $title;
}, 99999 );


// should append with " atl is defined" and does not - this is where I need help
add_filter( 'get_the_terms', function( $terms, $post_id, $taxonomy ) {
    if ( is_atl_defined() ) {
        foreach ( $terms as $term ) {
            $term->name = $term->name . " atl is defined";
        }
    }
    return $terms;
}, 99999, 3 );

0
Roz 4 weeks 2023-05-09T00:22:19-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse