• acorroy

    (@acorroy)


    Hello,

    i’ve a problem with archive pages on wordpress that generate 404’s for some years like :

    mysite.com/2020/ => it’s okay.
    mysite.com/2019/ => 404
    mysite.com/?year=2019 => 404
    mysite.com/?year=2020 => it’s okay

    My permalinks settings is : /%postname%/

    if i switch to basic permalinks like mysite.com/?p=123 :

    mysite.com/?year=2019 => it’s okay
    mysite.com/?year=2020 => it’s okay

    But i must keep permalinks to /%postname%/ so it’s not a solution.

    i disabled all plugins and i switched on basic theme and it is the same result.

    But the most strange is the query on mysite.com/2019/ , it’s like WP wanted to render a post and not archive.

    The bad query for mysite.com/2019/ :

    WP_Query Object
    (
        [query] => Array
            (
                [page] => 0
                [name] => 2019
            )
    
        [query_vars] => Array
            (
                [page] => 0
                [name] => 2019
                [error] => 
                [m] => 
                [p] => 0
                [post_parent] => 
                [subpost] => 
                [subpost_id] => 
                [attachment] => 
                [attachment_id] => 0
                [pagename] => 
                [page_id] => 0
                [second] => 
                [minute] => 
                [hour] => 
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] => 
                [tag] => 
                [cat] => 
                [tag_id] => 
                [author] => 
                [author_name] => 
                [feed] => 
                [tb] => 
                [paged] => 0
                [meta_key] => 
                [meta_value] => 
                [preview] => 
                [s] => 
                [sentence] => 
                [title] => 
                [fields] => 
                [menu_order] => 
                [embed] => 
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [post_name__in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
                [post_parent__in] => Array
                    (
                    )
    
                [post_parent__not_in] => Array
                    (
                    )
    
                [author__in] => Array
                    (
                    )
    
                [author__not_in] => Array
                    (
                    )
    
                [search_columns] => Array
                    (
                    )
    
            )
    
        [tax_query] => 
        [meta_query] => 
        [date_query] => 
        [post_count] => 0
        [current_post] => -1
        [before_loop] => 1
        [in_the_loop] => 
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 1
        [is_preview] => 
        [is_page] => 
        [is_archive] => 
        [is_date] => 
        [is_year] => 
        [is_month] => 
        [is_day] => 
        [is_time] => 
        [is_author] => 
        [is_category] => 
        [is_tag] => 
        [is_tax] => 
        [is_search] => 
        [is_feed] => 
        [is_comment_feed] => 
        [is_trackback] => 
        [is_home] => 
        [is_privacy_policy] => 
        [is_404] => 
        [is_embed] => 
        [is_paged] => 
        [is_admin] => 
        [is_attachment] => 
        [is_singular] => 1
        [is_robots] => 
        [is_favicon] => 
        [is_posts_page] => 
        [is_post_type_archive] => 
        [query_vars_hash:WP_Query:private] => a7f8850f3b459b7f64aad210f58ca122
        [query_vars_changed:WP_Query:private] => 
        [thumbnails_cached] => 
        [allow_query_attachment_by_filename:protected] => 
        [stopwords:WP_Query:private] => 
        [compat_fields:WP_Query:private] => Array
            (
                [0] => query_vars_hash
                [1] => query_vars_changed
            )
    
        [compat_methods:WP_Query:private] => Array
            (
                [0] => init_query_flags
                [1] => parse_tax_query
            )
    
    )

    The good query on mysite.com/2020/ :

    WP_Query Object
    (
        [query] => Array
            (
                [year] => 2020
            )
    
        [query_vars] => Array
            (
                [year] => 2020
                [error] => 
                [m] => 
                [p] => 0
                [post_parent] => 
                [subpost] => 
                [subpost_id] => 
                [attachment] => 
                [attachment_id] => 0
                [name] => 
                [pagename] => 
                [page_id] => 0
                [second] => 
                [minute] => 
                [hour] => 
                [day] => 0
                [monthnum] => 0
                [w] => 0
                [category_name] => 
                [tag] => 
                [cat] => 
                [tag_id] => 
                [author] => 
                [author_name] => 
                [feed] => 
                [tb] => 
                [paged] => 0
                [meta_key] => 
                [meta_value] => 
                [preview] => 
                [s] => 
                [sentence] => 
                [title] => 
                [fields] => 
                [menu_order] => 
                [embed] => 
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [post_name__in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
                [post_parent__in] => Array
                    (
                    )
    
                [post_parent__not_in] => Array
                    (
                    )
    
                [author__in] => Array
                    (
                    )
    
                [author__not_in] => Array
                    (
                    )
    
                [search_columns] => Array
                    (
                    )
    
            )
    
        [tax_query] => WP_Tax_Query Object
            (
                [queries] => Array
                    (
                    )
    
                [relation] => AND
                [table_aliases:protected] => Array
                    (
                    )
    
                [queried_terms] => Array
                    (
                    )
    
                [primary_table] => 
                [primary_id_column] => 
            )
    
        [meta_query] => 
        [date_query] => 
        [post_count] => 0
        [current_post] => -1
        [before_loop] => 1
        [in_the_loop] => 
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 
        [is_preview] => 
        [is_page] => 
        [is_archive] => 1
        [is_date] => 1
        [is_year] => 1
        [is_month] => 
        [is_day] => 
        [is_time] => 
        [is_author] => 
        [is_category] => 
        [is_tag] => 
        [is_tax] => 
        [is_search] => 
        [is_feed] => 
        [is_comment_feed] => 
        [is_trackback] => 
        [is_home] => 
        [is_privacy_policy] => 
        [is_404] => 
        [is_embed] => 
        [is_paged] => 
        [is_admin] => 
        [is_attachment] => 
        [is_singular] => 
        [is_robots] => 
        [is_favicon] => 
        [is_posts_page] => 
        [is_post_type_archive] => 
        [query_vars_hash:WP_Query:private] => db4eb8efff91c1617818d1aaa2f1c4a6
        [query_vars_changed:WP_Query:private] => 
        [thumbnails_cached] => 
        [allow_query_attachment_by_filename:protected] => 
        [stopwords:WP_Query:private] => 
        [compat_fields:WP_Query:private] => Array
            (
                [0] => query_vars_hash
                [1] => query_vars_changed
            )
    
        [compat_methods:WP_Query:private] => Array
            (
                [0] => init_query_flags
                [1] => parse_tax_query
            )
    
    )

    So, i make an “hack” to alter query and prevent this 404’s like :

    add_action('pre_get_posts', 'bad_archive_404_pre_get_posts');
    /**
     * @param WP_query $query
     */
    function bad_archive_404_pre_get_posts( $query ) {
        
        if ($query->is_main_query() && !is_admin() && isset( $query->query['name'] ) && is_numeric($query->query['name']) && isset( $query->query['page'] ) && $query->query['page'] === 0) {
    
            $year = $query->query['name'];
            $query->query['name'] = '';
            $query->query['page'] = '';
            $query->query['year'] = $year;
    
            $query->year = $year;
            $query->is_singular =false;
            $query->is_single =false;
            $query->is_archive = true;
            $query->is_year = true;
            $query->is_date = true;
    
    
            $query->query_vars['name'] = '';
            $query->query_vars['page'] = '';
    
            $query->set('year', $year);
    
        }
    
    }

    And it’s work.

    So my questions are :

    1. It is WP bug ?
    2. Why WP detect URI request like post instead archive ?
    3. May be there are most simple to resolve the 404’s on archive page ? (I do not feel comfortable to alter queries)

    Thanks for watching and help me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • threadi

    (@threadi)

    According to the 2019 query, you have no posts for 2019. Is that true? Is there really no contribution from 2019? Then a 404 for the 2019 page could definitely be explained.

    Thread Starter acorroy

    (@acorroy)

    I’ve more than 3000 posts created in 2019. After alter the query archive pages for 2019, i can see archive for 2019 like 2020.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Wrong detection of archive pages’ is closed to new replies.