Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#14275 closed defect (bug) (worksforme)

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

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

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: ↓ 2   sirono3 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   sirono3 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;

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