0

I need to check if a certain value is true in the post_meta right after the wp fetches a custom post from database, If it's false I need to modify/add some data to the post and then display the modified version, if it's true it should continue as usual. What would be the best hook and approach in general to accomplish something like this?

Edit: Forgot to mention before, but I would HIGHLY prefer to do this inside a plugin.

2 Answers 2

1

You can use the_content filter to modify content before it's output.

function my_the_content_filter( $content ) {
    // maybe limit to archive or single cpt display?
    if( is_post_type_archive( 'your-cpt' )
        || is_singular( 'your-cpt' ) ){
        global $post;
        $meta = get_post_meta( $post->ID, 'some-key', true );
        if( false === $meta ){
            return $content . 'some extra content';
        }
    }
    return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );
2
  • This would work, but what if I need to modify the meta along with content?
    – Leylu
    Jan 20, 2014 at 18:02
  • add a filter to get_post_metadata.
    – Milo
    Jan 20, 2014 at 18:26
0

I would do this inside of my single-custon_post_type.php, using the get_meta() function and conditional checks.

If the meta field is not set, or does not contain data you need - you can then display content in a separate way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.