Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress tries to get an archive of non-existing category

Tags:

wordpress

In short, I'm moved some articles from default post-type to custom post type. And now, my articles are accessible using an old www.example.com/articleurl/ and a new one www.example.com/blog/articleurl/ urls.

There is a problem: a client added an article with url '2093'. And it's accessible using www.example.com/blog/2093/. But when I try to get it using www.example.com/2093/ wordpress tries to get some archive of 2093 year or category(there is no such category, by the way) and then redirects to the index page.

So how to solve this?

No, I can't change url of this article. I need to leave it 2093.

Yes, I need this hierarchy of my site and I need custom post type.

like image 999
user64675 Avatar asked Aug 06 '16 19:08

user64675


1 Answers

Create a file called archive-2093.php in your theme root and add the following code:

<?php

$args = array (
    'name'         => '2093',
    'post_type'    => array( 'blog' ),
);


$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        //  adapt the following to suit the formatting and fields of your post.

        the_title();
        the_content();

    }
} else {
    // oh no, it didn't work.
}

// Restore original Post Data
wp_reset_postdata();

You'll have to adapt the loop to match whatever layout and structure you're using in single.php or blog-single.php. The concept here is to create a specific archive for the year 2093 and then just do a custom query to return your post.

Just remember to update your code in the year 2093.

like image 77
Dan Devine Avatar answered Nov 16 '22 03:11

Dan Devine