WordPress.org

Ready to get started?Download WordPress

Forums

All in One SEO Pack
Use new Custom Fields from post for Title? (4 posts)

  1. ericrw
    Member
    Posted 7 months ago #

    Does anyone know how I can use a custom field and insert it into the format settings?

    I have a custom field with key called 'map_location' and I am trying to add it into the title tag, along with other info.

    http://wordpress.org/plugins/all-in-one-seo-pack/

  2. Peter Baylies
    Member
    Plugin Author

    Posted 6 months ago #

    Hi ericrw,

    You may do this by writing some code to filter the title to add your tag; the filter hook is aioseop_title and it gets passed the current title by default. Here's an example with a custom field named my_tag and a replacement parameter in the title format named %my_tag%:

    add_filter('aioseop_title', 'sfwd_custom_title');
    
    function sfwd_custom_title($title) {
       global $post;
       if ( !empty( $post ) ) {
            $my_tag = get_post_meta( $post->ID, "my_tag", true );
            if ( !empty( $my_tag ) ) $title = str_replace( "%my_tag%", $my_tag, $title );
       }
       return $title;
    }
  3. numero7
    Member
    Posted 5 months ago #

    Hi i'm trying to do the same, but my custom fields are not stored in the post_meta table but in a separate table called "properties".

    for example, In my single page template i use the following code to retrieve the meta values

    $post_id = get_the_ID();
                        $prop = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}properties WHERE <code>post_id</code> = '$post_id' LIMIT 1",ARRAY_A);

    and
    echo $prop['city']

    to display the meta field "city" for example...
    Do you have any idea to make your function working with my custom table ?
    thanks a lot.

  4. Peter Baylies
    Member
    Plugin Author

    Posted 5 months ago #

    Hi numero7,

    This is fairly straightforward, you'd do that like so (example here is for city, replacement tag is %city% ):

    add_filter('aioseop_title', 'sfwd_custom_title');
    
    function sfwd_custom_title($title) {
       global $post;
       if ( !empty( $post ) ) {
            $prop = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}properties WHERE post_id = '{$post->ID}' LIMIT 1",ARRAY_A);
            $city = $prop['city'];
            if ( !empty( $city ) ) $title = str_replace( "%city%", $city, $title );
       }
       return $title;
    }

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.