Reputation: 6370
I have a plugin that automatically expires posts and changes it's post status to "archive" on a certain date.
We then have an archive section that houses all these posts. I still want to display the data for the post but it gives a page not found.
Can I alter the query to display this post on the single template?
I've tried the following but it pulls in all archive posts rather than just the page you're on.
<?php
$my_query = new WP_Query('post_status=archive');
?>
<div>
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
?>
<ul>
<li>
<?php the_title(); ?>
</li>
</ul>
<?php endwhile; else: ?>
<div>
<ul>
<li><?php _e('No upcoming Posts'); ?></li>
</ul>
</div>
<?php endif; ?>
</div>
Upvotes: 0
Views: 234
Reputation: 6370
Thanks to @josephting & @bodi0 for getting me thinking in a different way, actually solved it without adjusting the single template from the original loop by using this (I'd used this on another site to get future posts to show):
add_filter('the_posts', 'show_future_posts');
function show_future_posts($posts)
{
global $wp_query, $wpdb;
if(is_single() && $wp_query->post_count == 0)
{
$posts = $wpdb->get_results($wp_query->request);
}
return $posts;
}
With all other solutions either it was still saying page not found or wasn't displaying the data fully.
Upvotes: 0
Reputation: 2928
Display post by ID:
$query = new WP_Query( 'post_status=archive&p=7' );
Display page by ID:
$query = new WP_Query( 'post_status=archive&page_id=7' );
Upvotes: 0
Reputation: 2665
The reason why you get all archived posts is because you're querying an entirely new query with the post_status=archive
.
To solve this, I've been trying with some code to utilize WordPress Action API like pre_get_posts
but it doesn't work properly for me mainly because WordPress has already thrown a 404 error.
So it might be a better idea to override this in the template like in single.php
.
<?php
$post_id = get_query_var('p');
$args = array(
'p' => $post_id,
'post_status' => array( 'publish', 'archive' )
);
$my_query = new WP_Query($args);
?>
I might suggest to try this out first. As I mentioned, you were getting all the posts in archive. In this case, you are getting the post ID and adding that into the query. Therefore, you'll be getting on the current post and with publish
and archive
post_status so that all posts with both post_status will be queried.
Unfortunately, in my testing, it just doesn't work on visitors' who aren't logged in. It'll work for you who's logged in.
Let me hear about the result in the comment.
Upvotes: 1
Reputation: 31829
Post status allows users to set a workflow status for a post in WordPress.
There are 8 default statuses that WordPress uses. They are published
, future
, draft
, pending
, trash
, auto-draft
, and inherit
.
A post may also have a new
status if it was just created and hasn't had any previous status.
WordPress themes and plugins can also define custom post statuses for more complex websites. These statuses allows users to organize their posts in the admin panel. They are especially useful for sites with multiple authors or a complicated editorial process.
Some things that post status allows is for users to work on an article without publishing it and saving it as a draft. This way they can go back later and finish it. It also allows users to schedule posts which gives the post a status of future
, or make a post private. Attachments have a post status of inherit
. For multi author blogs the pending
status can be useful as contributors can submit posts for review prior to publishing.
In your case, you have to be 100% sure that your archived posts get status archive
, otherwise your WP query will not work, so check if this plugin you use set this post type (the easy way is to look directly in MySQL database, table wp_posts
or, if you do not have access to the MySQL
server via PHPMyAdmin
- then look at the source code of the plugin itself.).
EDIT: Can you try, just for testing, to change your WP query like this:
$my_query = new WP_Query( array( 'post_status' => array( 'pending', 'archive', 'publish' ) ) );
and see the result?
Upvotes: 1