Why Your Custom Post Type Archive Redirects to a Single Post
When you register a custom post type like news and set 'has_archive' => true, WordPress automatically registers an archive query variable for that slug. However, simply adding code to a template file is not enough; the routing engine must know which view to render when accessing /sitename/news. If you visit that URL and land on the single post page of your second item instead of seeing a list, the issue usually lies in how WordPress interprets the query variables or where your custom loop is being executed.
The most common culprit is the order of execution within wp-content/themes/your-theme/index.php (or archive-news.php). WordPress processes queries sequentially. If you have a global loop running at the top of your theme files that targets the current post, it can interfere with archive logic before your custom template loads.
The Query Logic Problem
In your provided code snippet, you are using wp_get_recent_posts(). While this function works for static displays, it does not interact well with the standard WordPress query loop unless explicitly told to do so. More critically, if you are manually constructing a list of posts without ensuring that the main query is empty or properly scoped, WordPress might still be trying to render the "featured" post based on global variables set by previous hooks.
However, the specific symptom of landing on a single post suggests that the archive page itself isn't rendering correctly, or the URL structure has been misinterpreted as a singular post request. If your news CPT is registered with 'has_archive' => 'news', the URL /sitename/news should trigger the archive-news.php template.
Implementing the Correct Template Hierarchy
To fix this, you need to ensure that WordPress loads the correct file for the archive. Create a new file named archive-news.php inside your active theme's root directory (e.g., wp-content/themes/your-theme/archive-news.php).
Inside this file, do not rely on global loops from parent files. Instead, initialize your own query using the WP_Query class or maintain the main query if you intend to use template tags. Your code should look like this:
<?php
// archive-news.php
$args = array(
'post_type' => 'news',
'posts_per_page' => 10, // Adjust pagination as needed
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<div class="news_releases">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<h5><i>Published On: <?php echo date('d-m-Y', strtotime(get_the_date())); ?></i></h5>
<p>
<?php
// Use excerpt_length for clean truncation or custom logic
the_excerpt();
?>
</p>
</div>
<?php
}
wp_reset_postdata();
}
Why wp_get_recent_posts() Can Be Tricky
Your original approach used wp_get_recent_posts(). This function is useful for static widgets or specific non-query contexts, but it bypasses the standard the_content filters and post status logic unless configured meticulously. When using this function, you must manually handle pagination and ensure that the global $post object isn't left in a state that confuses subsequent template tags.
Furthermore, truncating content with substr() and strip_tags() is a manual approach to excerpt generation. While functional, it lacks the safety checks of wp_trim_words or get_the_excerpt(). The latter respects the theme's excerpt settings and handles long strings gracefully without risking broken HTML if a post contains <br> tags inside a paragraph that gets cut off mid-sentence.
Checking Registration Arguments
Ensure that when you registered the CPT in functions.php, you did not accidentally set 'show_in_rest' or other flags that might interfere with URL rewriting. The standard registration should look like this:
register_post_type('news', array(
'labels' => array(
'name' => 'News',
'singular_name' => 'News Item',
),
'public' => true,
'has_archive' => 'news', // Explicitly setting the slug
'supports' => array('title', 'editor'),
'menu_icon' => 'dashicons-admin-post',
));
The "Second Post" Red Herring
If you are seeing the second post specifically, verify that your theme's index.php or front-page.php isn't hardcoding a loop that pulls two posts for the homepage and then accidentally redirects traffic. Sometimes, if .htaccess rules or permalinks are misconfigured, /news/123 (where 123 is the ID of the second post) might resolve to the single template, but /news without an ID should hit the archive.
If you have confirmed the file archive-news.php exists and contains the correct loop, flush your rewrite rules by visiting the WordPress admin panel, navigating to Settings > Permalinks, and clicking "Save Changes." This ensures the .htaccess or web.config rules correctly map the /news slug to the archive handler.
For more complex custom post type architectures involving multiple archives or specific query modifications, resources from LaravelCompany offer deeper insights into managing complex content structures in modern frameworks.