How to Prepend ‘Sponsored post:’ to post title using post_meta

Simple little snippet that will use a custom field sponsored
set to true will prepend “‘Sponsored post:” to the_title
. The second snippet will prepend the value of sponsored
to the_title
.
wordpress snippet : PHP
<>
add_filter( 'the_title', 'wps_sponsored' ); function wps_sponsored( $title ) { global $post; $sponsored = get_post_meta($post->ID, 'sponsored', true); if( is_single() && $sponsored == 'true' ){ return 'Sponsored post: '.$title; } return $title; }
The second snippet will prepend whatever value you add to the sponsored
custom field to the_title
.
wordpress snippet : PHP
<>
add_filter( 'the_title', 'wps_sponsored' ); function wps_sponsored( $title ) { global $post; $sponsored = get_post_meta($post->ID, 'sponsored', true); if( is_single() && !empty($sponsored) ){ return $sponsored.' '.$title; } return $title; }
( WordPress codex functions, hooks, in this snippet. )
the_title, get_post, is_single, get_post_meta, add_filter, wp,