Is there a way to get the content from another outside the loop? The ID is 302 and I need to display the content of that on another page.
11 Answers
You can use get_page()
to return the $post
object of a static page:
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Edit
Similarly, you can use get_post()
to return the $post
object of a post:
$post_id = 302;
$post_object = get_post( $post_id );
echo $post_object->post_content;
-
-
1Just found out it doesn't. Use
setup_postdata( $post );
first, after that, you can usethe_content();
– Tim BaasCommented Feb 20, 2014 at 21:39 -
6You could just run
apply_filters( 'the_content', $post_object->post_content );
Commented Mar 1, 2017 at 7:30
to get the content of the post outside the loop you can write something like this
global $post;
$content = $post->post_content;
if ( !empty( $content ) ) :
echo $content;
endif;
-
global $post
is only available inside the loop, which is the whole point of the question– dtwCommented Jun 7, 2023 at 9:37 -
No, this is available outside the loop. Its a global variable and holds the current post data. codex.wordpress.org/Global_Variables Commented Jun 12, 2023 at 7:15
-
Sorry, my comment wasn't clear enough. Yes, it is a global variable but note that it is under the section titled "Inside the Loop variables", which says "While inside the loop, these globals are set, containing information about the current post being processed." If your code runs on a page before the loop, which $post is it going to return? Also, the OP wants to get content for a post, which is NOT the current post, so it doesn't even make sense in this case.– dtwCommented Jun 13, 2023 at 11:51
Was looking for the same thing, I'm surprised nobody said this:
$pageID = 302;
echo get_the_content(null, false, $pageID);
Works good! :)
-
1
If your content include shortcodes, you should use:
$post_id = 22;
$post_object = get_post( $post_id );
echo do_shortcode( $post_object->post_content );
-
Don't do shortcode kovshenin.com/2013/dont-do_shortcode Commented Jun 24, 2015 at 21:52
-
2That page doesn't provide a compelling reason to avoid
do_shortcode
other than saying, "It might be slow" and "You can just use the function that the short code calls." Those reasons are only valid for the most trivial for the most simple scenarios. It fails when the shortcode is generated somewhere else, or if there are multiple shortcodes, or shortcodes mixed in with other content and markup. Without a compelling reason, I would disagree with the statement to avoiddo_shortcode
. This smells like premature optimization.– JeffCommented Apr 17, 2017 at 21:35 -
And it also increases the coupling between your code and the original shortcode's code. If the shortcode's backing method is ever renamed or the signature changes, your code will break.– JeffCommented Apr 17, 2017 at 21:37
For completeness, building on Tim's comment above and inspired by Stephen Harris's article, the solution that enables use of the_content()
is:
$post_id = 302;
global $post;
$post = get_post($post_id);
setup_postdata( $post );
the_content();
wp_reset_postdata( $post );
And hence filters get applied (paragraphs will be inserted etc.) and shortcodes work.
-
This almost crashed my browser :/ taking out the ampersand helps, but it's still not parsing shortcodes.– ZadeCommented Oct 5, 2015 at 8:12
You can use, as said, the solution with get_post
and $post_object->post_content
, but don't forget to add a check before you use that post object:
function get_post_content( $post_id = null ) {
$post_object = get_post( $post_id );
if ( ! $post_object ) { return ''; }
//else
return apply_filters('the_content', $post_object->post_content);
}
echo get_post_content( $other_post_id );
Since you know your target post ID (302), you may find useful this shorthand syntax that you can use out of the loop (though its performance is pretty much the same as in any other alternative method:)
echo(get_post_field('post_content',302));
You can use the get_post_data()
function to get post outside the loop. Place this code in functions.php
function get_post_data($postId) {
global $wpdb;
return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId");
}
and then add this snippet for more control on the process
<?php $data = get_post_data(302);
echo $data->post_date; // post date
echo $data->post_title; // post title
echo $data->post_content; // post content
echo $data->comment_count; // comments number
?>
You can simply call get_the_content(postId)
<?php echo get_the_content($postId); ?>
-
3Actually, that's not possible. First arg there is for the content when there is a more link: codex.wordpress.org/Function_Reference/get_the_content Commented Aug 24, 2017 at 22:40
use wp_reset_postdata();
it will work.. (edited)
<?php
$args = array(
'post_type' => 'posttype',
'p' => 'post_id'
);
$the_query = new WP_Query( $args );
if( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif;
wp_reset_postdata();
?>
posttype can be "post" , "page" or your custom post type. Here p=302 is your post id.. Hope it will work.
-
2Never use
query_posts
unless you need to break page functionalities. Always useWP_Query
orget_posts
for custom queries :-) Commented Apr 4, 2015 at 6:55 -
yes.. you are right.. It can Wp_Query as well.. same result found.. Commented Apr 5, 2015 at 19:23
-
There is also the
pre_get_posts
filter, andthe_post
. So much detail. Commented Mar 1, 2017 at 7:35
you can put content in a category X and use query_post before while like this :
<?php query_posts('cat=X&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?= get_the_content(); ?>
<?php endwhile; ?>
get_queried_object_id()
! developer.wordpress.org/reference/classes/wp_query/…