Display current post on single template - php

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>

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?

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.

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' );

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.

Related

the_content() AND apply_filters('the_content'... ) not working in second loop on the page

I have a problem with a website that has been working for YEARS. Suddenly something has changed (no plugins were added).
Basically, I have a "post" page that displays 1 post at the top and then a couple of posts later down the page. The problem I have is that in these secondary posts, after establishing a new wp_query object, the "the_content()" and even trying to manually dump "$second_loop_post->post_content" into an apply_filters function returns nothing. The "the_content()" returns NULL. The kicker? the_title() and every other call works just fine for pulling out information. I can even pull out the unformatted info. But there seems to be something wrong with basic "apply_filters" which is also affecting "the_content()".
See below. Basically, when I load the page and try to do a custom query (even without displaying anything beforehand), it won't work. If I remove the custom $wp_query below however, it will spit out the information for that post page. The problem is I need the second and third queries I do to also display this content information .... and nothing. I'm going crazy. Help please.
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'posts_per_page' => 1
) );
if (have_posts()):
while (have_posts() ) : the_post();
the_title();
the_content();
endwhile;
endif;

Display custom registered posts in the custom query new WP_Query

I'm building a custom plugin for WordPress to display some custom posts.
So far so good, it worked (I had to re-save the permalinks), and now I'm able to see it in my archive page and, since I added the slug in the array for the register_post_type function, I can see them adding the slug in the url (for example: mywebsite.com/coolposts:
'rewrite' => array( 'slug' => 'coolposts' ),
So far so good but I have a problem and I don't know how to solve it.
I want to display my custom posts "coolposts" in my custom query:
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50');
while ($the_query->have_posts()) :
$the_query->the_post();
if ((($the_query->current_post+1) % 3 == 0) && ($the_query->current_post+1 !== count($the_query->posts))):
echo "</div><div class='row'>";
endif;
endwhile;
Because, for some reason, my custom posts are excluded from the main query.
Am I doing something wrong?
Just to be clear, I don't want to display ONLY my customs posts, I want to display all of my posts.
I assume you registered custom post type with the name coolposts when registered with register_post_type function. If so then just add another param post_type=coolposts. To avoid unexpected situation you can add one more param post_status=publish, this param will ensure only published posts are displayed.
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50&post_type=coolposts');

Using ACF fields in archive page for custom posts

I've created a custom posts called Projects and created a Projects template inside of archive-projects.php. I've also created a single-projects.php to display individual project. However, this became a problem when I had to use an ACF on the Projects template. I want to display a featured project on the Project page and also list out few of the posts I've created for projects on the same page. Below is my code to get featured project image.
<?php echo get_field('featured_project'); ?>
<?php
$featured_project = get_field('featured_project');
if ($featured_project):
$project = $featured_project;
setup_postdata( $project );
if ( has_post_thumbnail($featured_project->ID) ) { // check if the post has a Post Thumbnail assigned to it.
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($featured_project->ID), 'large' );
$featured_image = $featured_image[0];
}
?>
I've read few articles and similar questions on Stackoverflow but haven't found an answer yet. Has anyone been successful figuring this out?
You can try to use my plugin ACF CPT Options Page
That creates options page for each custom post type, then you can add Relationship Field to get posts easily

Create Manual Pagination for WordPress Custom Post Type

I need to create manual pagination for WordPress custom post type; situation is explained below.
custom post type is 'survey'
I want each survey question to be displayed on single page and then upon clicking next button, the page should be reloaded and the next question should be displayed, So survey's structure should be like
Starting Page: http://www.example.com/survey-title/
Question 1: http://www.example.com/survey-title/q/1/
Question 2: http://www.example.com/survey-title/q/2/
Question 3: http://www.example.com/survey-title/q/3
End Page: http://www.example.com/survey-title/result/
On the bases of question number(q) passed, I can retrieve the required question.
What are the possible options (without possibly modifying the core wp structure) to achieve this?
P.S. it's intended to be similar functionality that we get by using <!--more--> tag to split the post/page contents into multiple paginated pages.
Any help regarding is highly appreciated.
You basically need to create a page to loop your posts with the custom type "survey", give it a number of posts per page and add a small trick to make the pagination work.
Sounds simple? Here's translated in code:
$args = array(
'post_type' => 'survey', //your post type
'posts_per_page' => 1 // number of posts you want to see per page, in your case, 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content()
echo '</div>';
endwhile;
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Next Page' ); ?></div>
Are you posting data on every page or saving to POST only at the end? if it's the last case, this is not going to work.

Wordpress: Show custom post type details page

I have created a custom post type named as product. After that I have created a template file to show all products on that page and write the below code :
<?php $loop = new WP_Query( array( 'post_type' => 'acme_product',
'posts_per_page' => 14 ) );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<div>
<?php the_post_thumbnail('thumbnail');
?>
</div>
<div>
<?php the_title( '<h2 class="entry-title"><a href="'.get_permalink().'" title="'.
the_title_attribute( 'echo=0' ).
'" rel="bookmark">', '</a></h2>' );
?>
</div>
</div>
<?php endwhile;
?>
But when I click on the product title link it will not show my product details page. Can anyone help me?
You need to save your template as single-{post_type}.php so in your case it would be single-acme_product.php and make sure that permalink is enable if not than
Login to your WordPress admin.
Go to Settings -> Permalinks. And under Common Settings, let’s use Post name.
Then click ‘Save Changes’
as per your code you don't need to define the post_type here. just use with simple code
<?php while ( have_posts() ) : the_post(); ?>
It sounds as though you've already figured out how to loop through your product type. If you're new to custom post types and are not already aware of it, I would suggest investigating the "archive" template for custom post types (archive-{post_type}.php). I saw another answer that referenced single-{post_type}.php as well; both of these can be identified in the Template Hierarchy documentation in the WP Codex. By default, if you don't provide a customized single template for your post type, it will fall back to single.php (which may require customization if you want it to perform double-duty for multiple post types).
With that in mind, and assuming you're already using single-product.php in your theme (or have otherwise coaxed Wordpress into using your template), it would be helpful to know the symptoms you're experiencing:
Are you getting a 404? This could be a permalink issue, and if you haven't already attempted it you might consider flushing your rewrite rules. This can be accomplished by simply visiting the Settings -> Permalinks panel in the Dashboard.
Is your template being used? Consider adding some HTML comments to your template, and view source on the rendered version to ensure that you're even using the correct file. Failure to load the appropriate template could be due to a filename typo or some other logic that interferes with the normal template selection logic.
If you're getting the correct template, but you're not able to display the correct details, are you doing anything particularly wonky with your single template's loop?
Without clear details on what symptoms you're actually experiencing, it's hard to give you better direction. Hopefully some of this information points you to the correct answer, but if not please share additional details as to what is happening on your end.

Categories