• Here’s the description of “the_post” action from the WordPress Codex:

    The ‘the_post’ action hook allows developers to modify the post object immediately after being queried and setup.

    The post object is passed to this hook by reference so there is no need to return a value.

    However, I can’t seem to add an extra item to the post object like this:

    function my_the_post_action( $post_object ) {
    
    	$post_object->foo = 'bar';
    
    }
    add_action( 'the_post', 'my_the_post_action' );

    I’ve researched this and gotten into the WP Core code to try and figure it out, but nothing has worked. I thought that maybe I needed to run setup_postdata($post) after I got the post, but that did not work either.

    I don’t know what I’m doing wrong here. Has anybody else seen this problem?

Viewing 1 replies (of 1 total)
  • Thread Starter jhned

    (@jhned)

    Ok, nevermind. I did get it to work by using setup_postdata, but then I’d have to use it every time I used get_posts, which seems redundant.

    I tried using do_action('the_post') instead of setup_postdata, but that did not work. Looking to find a way to make it more efficient.

Viewing 1 replies (of 1 total)
  • The topic ‘the_post action does not modify the post object’ is closed to new replies.