Question

I want to create a shortcode that pulls value from a custom field of a post and parses that value on Post title, post content.

So if the custom field value is Icecream for Post 1.

On the admin side the post title will be Favorite [geo_name], it will be displayed as Favorite Icecream.

Here is my code so far:

function geo_name_function( $atts ) {

    $atts = shortcode_atts( array(
        'post_id' => get_the_ID(),
    ), $atts, 'geo_name' );

    return get_post_meta( $atts['post_id'], 'field_name', true );
}

add_shortcode('geo_name', 'geo_name_function');

add_filter( 'the_title', function( $geo_name_function ) {
    return do_shortcode( $geo_name_function );
});

I keep having one issue.

If Recent Posts Widget is on one of the posts it displays the same value for all post titles.

For example:

Article 1

[geo name] value = Geo 1

Admin title: [geo_name] title Rendered title: Geo 1 title

Article 2

[geo name] value = Geo 2

Admin title: [geo_name] title Rendered title: Geo 2 title

If I am on an Article 1 page my Recent Post Widget looks like:

Recent Posts

Geo 1 title -> url to article 1

Geo 1 title -> url to article 2

If I am on an Article 2 page my Recent Post Widget looks like:

Recent Posts

Geo 2 title -> url to article 1

Geo 2 title -> url to article 2

My PHP knowledge is very limited, Please assist!

Was it helpful?

Solution

the_title filter takes two parameters, one of them is the ID of the post for which it was called. Something like that should work.

add_filter( 'the_title', 'se385007_title_filter', 20, 2 );
function se385007_title_filter( $title, $post_id ) 
{
    $new_title = str_replace( "[geo_name]", "[geo_name post_id=$post_id]", $title );
    return do_shortcode( $new_title );
)

OTHER TIPS

I believe that's expected. get_the_ID() returns post_ID from main query, and in a single page will return post_ID for that page. The way you filter post title affects all post title that'll be rendered in a page regardless of their actual post_ID. Since the problem is in a widget, you need to get the ID from its query (not main one).

I'd try to use the following instead (untested):

function geo_name_function( $atts ) {

    $atts = shortcode_atts( array(
        'post_id' => get_queried_object_id(),
    ), $atts, 'geo_name' );

    return get_post_meta( $atts['post_id'], 'field_name', true );

}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top