WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#14275 closed defect (bug) (worksforme)

excerpt_more filter does not follow $post even after setup_postdata($post)

Reported by: jimmynguyc Owned by:
Milestone: Priority: normal
Severity: trivial Version: 3.0
Component: Query Keywords:
Focuses: Cc:

Description

A simple test by returning the $post-ID in the filter function and this is what I get :

// Default single post with ID 22

if (have_posts()) : while (have_posts()) : the_post();
    the_content(); 
endwhile; endif;


// Additional custom query
add_filter('excerpt_more',create_function('',"return $post->ID;"));

$myposts = get_posts($args);

foreach ($myposts as $post) : setup_postdata($post);
    echo $post->ID; // gives 519
    the_excerpt();  // shows 22 instead of 519
endforeach;

Change History (3)

comment:1 follow-up: @sirono5 years ago

  • Resolution set to worksforme
  • Severity changed from normal to minor
  • Status changed from new to closed

This is because setup-postdata() doesn't set the global $post variable. this is done in WP_Query().the_post();

comment:2 in reply to: ↑ 1 @sirono5 years ago

  • Cc sirono added
  • Component changed from Formatting to Query
  • Severity changed from minor to trivial

add the "global $post;" call directly before your foreach loop:
this should work :)

Default single post with ID 22

if (have_posts()) : while (have_posts()) : the_post();

the_content();

endwhile; endif;

Additional custom query
add_filter('excerpt_more',create_function(,"return $post->ID;"));

$myposts = get_posts($args);

global $post;

foreach ($myposts as $post) : setup_postdata($post);

echo $post->ID; gives 519
the_excerpt();
shows 22 instead of 519

endforeach;

comment:3 @nacin5 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.