I want to add a custom field for certain posts that I create that will automatically add a custom prefix for some short reads I publish, these will also be added to their own parent category.

The prefix will be "[30 Second Read]:" - and it to appear at the beginning of all posts I specify.

I don't know a great deal about custom fields to know how to do this.

  • Show us what you've tried till now. Obviously some code !!!
    – CodeMascot
    Sep 19 '17 at 1:51

There are probably a few ways to handle this. Here's one way using a checkbox to enable the prefix for individual posts, and a filter on the_title to add the prefix whenever the_title() is called for those posts.

1. Add a meta box for an on/off checkbox

a. Add the meta box to the post edit screen

function wpd_title_prefix_register_meta_box() {
        'Title Has Prefix?',
add_action( 'add_meta_boxes', 'wpd_title_prefix_register_meta_box' );

b. Render the meta box

function wpd_title_prefix_meta_box( $post ){
    $checked = get_post_meta( $post->ID, '_wpd_title_prefix', true );
    <input type="checkbox" name="wpd-title-prefix" <?php checked( $checked ); ?> /> Yes

c. Save the meta box value

function wpd_title_prefix_save_meta( $post_id ) {
    if( isset( $_POST['wpd-title-prefix'] ) ){
        update_post_meta( $post_id, '_wpd_title_prefix', 1 );
    } else {
        delete_post_meta( $post_id, '_wpd_title_prefix' );
add_action( 'save_post', 'wpd_title_prefix_save_meta' );

2. Filter the_title and add the prefix to posts that were checked

function wpd_title_prefix_filter( $title, $post_id ) {
    if( $checked = get_post_meta( $post_id, '_wpd_title_prefix', true ) ){
        $title = '[30 Second Read]: ' . $title;
    return $title;
add_filter( 'the_title', 'wpd_title_prefix_filter', 10, 2 );
  • Thank you for your suggestion. I'm pretty new to WP development so you'' have to break this down a little more for me. Is this a solution that will work in the 'custom fields' box? That's all I have to play with. There are hundreds of existing posts that I'm guessing I can't apply this to, which is fine, but the posts are incoming short news bulletins via an RSS feed which means there won't be any human intervention when these prefixes are added to the titles.
    – JS7319
    Sep 22 '17 at 21:48
  • The two parts to this are basically 1. saving some type of data to designate which posts get the prefix, and 2. applying the prefix to titles based on some sort of criteria. The first part above creates a meta box to provide a control for saving some data, the built in Custom Fields box is a type of meta box. The data ends up in the same place in either case- the metadata table. Maybe a better solution is a category instead of metadata. The second part will be similar regardless if it's metadata or a category- just the if condition you check would change.
    – Milo
    Sep 23 '17 at 1:46
  • So as I understand it, I would create an identifiable component to the posts that need to have this prefix, can this be all posts in a certain category, because that can work for me, alongside creating a custom field. And then the code will search for the criteria and amend the title accordingly with the prefix - is that right?
    – JS7319
    Sep 24 '17 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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