Probleme when i use setup_postdata on post_id
-
In my code, i use this :
<?php foreach ( $my_posts as $post ): setup_postdata( $post ); ?> <?php get_template_part( 'my_template' ) ?> <?php endforeach; ?>
It’s work but i have this warning with your plugin :
Warning: Attempt to read property “ID” on int in …/plugins/content-control/inc/functions/query.php on line 195
Maybe the solution will be to change your code on line 195 with this :$current_post_id = isset( $post->ID ) ? $post->ID : null;
Or anything else to check if the global post is a object or just an ID.
If i miss something in my code let me know, it’s possible i made a mistake
Thanks a lot
Matt
-
@karbou – Curious. Are you calling
wp_reset_postdata();
after your loop to properly reset the global postdata when your done?I don’t see that in your code but should occur after the foreach ends.
That aside are you calling for the_content or excerpt in the template?
Can you provide a more complete example to replicate the issue and work out how to resolve it properly, including the minimal amount of template and loop code needed to throw an error.This is my code :
<?php foreach( $my_posts as $post ): setup_postdata( $post ); ?> <?php get_template_part( 'card' ) ?> <?php endforeach; ?>
and in the end of my template ‘card’, i have
wp_reset_postdata
I try to add
wp_reset_postdata
directly after the foreach, but changed anything;
The funny fact is this code work perfecly, its the good post display in my template, but with the warning@karbou – Hmm that code already checks post is set correctly.
The global
$post
var itself should never be an int, always theWP_Post
object, all global functions likeget_the_ID()
rely on this so checking if its already an ID seems inappropriate.This is
get_the_ID()
for reference:return ! empty( $post ) ? $post->ID : false;
But for the sake of testing, I set up a restriction on posts and set them to show a custom message if restricted.
I then made these 6 shortcodes and inserted them all onto pages, I also installed Query Monitor to check for errors during page rendering.
I was not able to produce the error or warning you mentioned. Maybe you can work out an example shortcode that reproduces it reliably.
It should be noted that only the ones using the global $post overrides, and using
the_content()
orapply_filters('the_content', get_the_content(
applied restrictions to content.
—
Even with all these running on a single page, the function on the line you pointed to only gets called twice, neither time was $post improperly set, even when I set it to an ID (in the last 3) this error doesn’t come up.My guess is you have another plugin also interacting with the global $post in some way that makes this occur only for you. If you can narrow it down using the Health Check & Troubleshooting plugin’s Troubleshooting mode that might help: https://www.ads-software.com/support/topic/php-warnings-169/#post-17074537
Let me know.
add_shortcode('ca_test_shortcode', function () { // Fetch some posts to test with. Adjust the query as needed. $args = array( 'posts_per_page' => 5, // Change this number to adjust the number of posts 'post_type' => 'post', ); $my_posts = get_posts($args); // Start buffering output ob_start(); global $post; $old_post = $post; echo '<h4>setup_postdata( $post ) - global $post overload</h4>'; echo "<ul>"; // Loop through the posts and apply the user's setup foreach ( $my_posts as $post ) { setup_postdata( $post ); // You can include a template part or any other content here echo '<li>- ' . get_the_title() . '<br/>'; the_content(); echo '</li>'; // Example: Display the title } // Reset postdata to ensure global $post returns to its original state wp_reset_postdata(); echo "</ul>"; $post = $old_post; // Get the buffered content $output = ob_get_clean(); return $output; } ); add_shortcode('ca_test_shortcode2', function () { // Fetch some posts to test with. Adjust the query as needed. $args = array( 'posts_per_page' => 5, // Change this number to adjust the number of posts 'post_type' => 'post', ); $my_posts = get_posts($args); // Start buffering output ob_start(); echo '<h4>setup_postdata( $post ) - only</h4>'; echo "<ul>"; // Loop through the posts and apply the user's setup foreach ( $my_posts as $post ) { setup_postdata( $post ); // You can include a template part or any other content here echo '<li>- ' . get_the_title() . '<br/>'; the_content(); echo '</li>'; // Example: Display the title } echo "</ul>"; // Reset postdata to ensure global $post returns to its original state wp_reset_postdata(); // Get the buffered content $output = ob_get_clean(); return $output; }); add_shortcode('ca_test_shortcode3', function () { // Fetch some posts to test with. Adjust the query as needed. $args = array( 'posts_per_page' => 5, // Change this number to adjust the number of posts 'post_type' => 'post', ); $my_posts = get_posts($args); // Start buffering output ob_start(); echo '<h4>setup_postdata( $post ) - pass $post to get_the_title</h4>'; echo "<ul>"; // Loop through the posts and apply the user's setup foreach ( $my_posts as $post ) { setup_postdata( $post ); // You can include a template part or any other content here echo '<li>- ' . get_the_title( $post ) . '<br/>'; echo apply_filters( 'the_content', get_the_content( null, null, $post ) ); echo '</li>'; // Example: Display the title } echo "</ul>"; // Reset postdata to ensure global $post returns to its original state wp_reset_postdata(); // Get the buffered content $output = ob_get_clean(); return $output; }); add_shortcode('ca_test_shortcode4', function () { // Fetch some posts to test with. Adjust the query as needed. $args = array( 'posts_per_page' => 5, // Change this number to adjust the number of posts 'post_type' => 'post', 'field' => 'ids', ); $my_posts = get_posts($args); // Start buffering output ob_start(); echo '<h4>setup_postdata( $id ) - only</h4>'; echo "<ul>"; // Loop through the posts and apply the user's setup foreach ( $my_posts as $post ) { setup_postdata( $post ); // You can include a template part or any other content here echo '<li>- ' . get_the_title() . '<br/>'; the_content(); echo '</li>'; // Example: Display the title } echo "</ul>"; // Reset postdata to ensure global $post returns to its original state wp_reset_postdata(); // Get the buffered content $output = ob_get_clean(); return $output; }); add_shortcode('ca_test_shortcode5', function () { // Fetch some posts to test with. Adjust the query as needed. $args = array( 'posts_per_page' => 5, // Change this number to adjust the number of posts 'post_type' => 'post', 'field' => 'ids', ); $my_posts = get_posts($args); // Start buffering output ob_start(); echo '<h4>setup_postdata( $id ) - pass $id to get_the_title</h4>'; echo "<ul>"; // Loop through the posts and apply the user's setup foreach ( $my_posts as $post ) { setup_postdata( $post ); // You can include a template part or any other content here echo '<li>- ' . get_the_title( $post ) . '<br/>'; echo apply_filters( 'the_content', get_the_content( null, null, $post ) ); echo '</li>'; // Example: Display the title } echo "</ul>"; // Reset postdata to ensure global $post returns to its original state wp_reset_postdata(); // Get the buffered content $output = ob_get_clean(); return $output; }); add_shortcode('ca_test_shortcode6', function () { // Fetch some posts to test with. Adjust the query as needed. $args = array( 'posts_per_page' => 5, // Change this number to adjust the number of posts 'post_type' => 'post', 'field' => 'ids', ); $my_posts = get_posts($args); // Start buffering output ob_start(); global $post; $old_post = $post; echo '<h4>setup_postdata( $id ) - only</h4>'; echo "<ul>"; // Loop through the posts and apply the user's setup foreach ( $my_posts as $post ) { setup_postdata( $post ); // You can include a template part or any other content here echo '<li>- ' . get_the_title() . '<br/>'; the_content(); echo '</li>'; // Example: Display the title } echo "</ul>"; // Reset postdata to ensure global $post returns to its original state wp_reset_postdata(); $post = $old_post; // Get the buffered content $output = ob_get_clean(); return $output; });
- The topic ‘Probleme when i use setup_postdata on post_id’ is closed to new replies.