bbruman bbruman - 4 months ago 10
PHP Question

Setting WordPress Title into Two Separate Variables

I have a custom post type that has a bunch of posts all formatted like so

Artist - Song Title


for example

The Smashing Pumpkins - Quiet


I am trying to put 'Artist' in a variable $artist and 'Song Title' in a variable $song

$artistsong = get_the_title();
$songeach = explode("-", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];


But this does not work. Echo-ing $artist gets the full title

The Smashing Pumpkins - Quiet


and echoing $song does not output anything

This works if I am just starting from plaintext, but not with 'get_the_title()'

$song = "The Smashing Pumpkins - Quiet";
$songeach = explode("-", $song);
$artist = trim($songeach[0]);
$song = trim($songeach[1]);
echo $artist;
//echos 'The Smashing Pumpkins'
echo $song;
//echos 'Quiet'


Is there another way to put the full title into a variable initially other than get_the_title() which does not seem to be working for me, or am I missing something else?

Answer

Add this code to your functions.php

function get_the_title_keep_hyphen( $post = 0 ) {
    $post = get_post( $post );

    $title = isset( $post->post_title ) ? $post->post_title : '';
    $id = isset( $post->ID ) ? $post->ID : 0;

    if ( ! is_admin() ) {
        if ( ! empty( $post->post_password ) ) {

            /**
             * Filter the text prepended to the post title for protected posts.
             *
             * The filter is only applied on the front end.
             *
             * @since 2.8.0
             *
             * @param string  $prepend Text displayed before the post title.
             *                         Default 'Protected: %s'.
             * @param WP_Post $post    Current post object.
             */
            $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
            $title = sprintf( $protected_title_format, $title );
        } elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {

            /**
             * Filter the text prepended to the post title of private posts.
             *
             * The filter is only applied on the front end.
             *
             * @since 2.8.0
             *
             * @param string  $prepend Text displayed before the post title.
             *                         Default 'Private: %s'.
             * @param WP_Post $post    Current post object.
             */
            $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
            $title = sprintf( $private_title_format, $title );
        }
    }

    /**
     * Filter the post title.
     *
     * @since 0.71
     *
     * @param string $title The post title.
     * @param int    $id    The post ID.
     */
    return $title;
}

And use this code in your single.php

$artistsong = get_the_title_keep_hyphen();
$songeach = explode(" - ", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];

See the last line

I change from return apply_filters( 'the_title', $title, $id ); to return $title;

Because apply_filters function change the hyphen from - => .

Comments