1

The Challenge

I have been trying to write a filter that applies (prepends a string) to (a) post titles of a certain post type and (b) to that same post type WordPress admin backend for use by an auto social sharing plugin.

I've gotten really close. I've achieved: 1. prepending only to custom post_type post titles 2. prepending to custom_post type titles AND all nav menu items 3. prepending to ALL titles (post, page, nav menu, and backend auto-sharing plugin)

The final, 3rd attempt got me very hopeful, but yet still, no matter the function, conditional statement, or combination I use, I can't get custom_post title AND backend social sharing plugin, but not nav menu items.

My Code - what I've tried

I've tried different conditionals, like is_admin() and !is_nav_menu with all nav menu IDs. I've also tried comparing the ID of the post to the ID of the menu. Finally, I've also tried adding the filter only to loop_start and that seems promising if combined with other statements. I will clean up my code for your review but I leave it this way for now in hopes it helps to see what I've tried and maybe where I went wrong with any of those methods.

// , $id = NULL
function append_album_review_to_title( $title ) {
    global $post;
    global $dont_apply_title_filter;
    $text = 'Album Review: ';
    $prepended_title = $text . $title;
/*
    $nav_menus_obj = wp_get_nav_menus();
    $nav_menu_ids = '';
    foreach( $nav_menus_obj as $nav_menu ) {
        $nav_menu_ids .= $nav_menu->term_id . ',';
    }
    rtrim($nav_menu_ids, ',');
*/
//  if ( get_post_type( $post->ID ) == 'album_review' && in_the_loop() ){
//  if ( get_post_type( $post->ID ) == 'album_review' && $id == get_the_ID() ){
//  if ( !is_nav_menu( '32057,32058,35135,32054,32056' ) ) {
//  if ( get_post_type( $post->ID ) == 'album_review' && is_nav_menu( $id ) ) {
    if ( get_post_type( $post->ID ) == 'album_review' && !$dont_apply_title_filter ) {
        //print_r( $nav_menu_ids );
        //print_r( is_nav_menu( $nav_menu_ids ) );
        return $prepended_title;
/*
    } elseif ( get_post_type( $post->ID ) == 'album_review'  ) {
        return $title;
*/
    } else {
        return $title;
    };
}
add_filter('the_title', 'append_album_review_to_title');
//add_action('save_post', 'custom_post_type_title', 100);
/*
function set_custom_title() {
    add_filter( 'the_title', 'append_album_review_to_title', 10, 2 );
}
add_action( 'loop_start', 'set_custom_title' );
*/
  • Could you please avoid adding the same question + answer over and over again? Please see How to Ask for guidance and take the tour. Thanks. – kaiser Mar 14 '16 at 16:14
0

Solution

function append_album_review_to_title( $title, $id = NULL ) {
    if ($id) {
        if ( get_post_type( $id ) == 'album_review' ){
            return 'Album Review: ' . $title;
        } else {
            return $title;
        }
    } else {
        return 'Album Review: ' . $title;
    };
}
add_filter('the_title', 'append_album_review_to_title', 10, 2);

Explanation

First trying this:

function append_album_review_to_title( $title, $id ) {
    if ( get_post_type( $id ) == 'album_review' ){
        return 'Album Review: ' . $title;
    } else {
        return $title;
    }
}
add_filter('the_title', 'append_album_review_to_title', 10, 2);

I noticed that on the backend, the social sharing plugin I am using to auto-share posts would return warnings like missing parameter 2 and Notice: Trying to get property of non-object..., and so it occurred to me that unlike the front end (nav menu and the loop), in the backend, apparently an $id is not being passed to the_title and so I can use this as a conditional.

Checking for $id will, for my purposes, tell me that if it is true, we are on front end, if it is false, then we are at backend post view.

This code accomplishes what I need (ie, modify post title in the loop, do NOT modify nav menu items, and modify the_title for use by a backend social sharing plugin):

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.