Auto-Generating Meta Titles with PODS Custom Fields
-
Hi Sybre,
I’m attempting to programmatically generate meta titles for all posts with the “product” custom post type on my website.
I’d like the meta title to be a concatenation of several custom fields from the PODS plug-in.
I’m following your answer to this post on using PODS fields to generate titles and descriptions.
I’ve created the following snippet to generate meta titles for products:
add_filter( 'the_seo_framework_title_from_custom_field', function( $title, $args ) { // A custom title is found for TSF already, let's skip further processing. if ( $title ) return $title; // If Pods is deactivated, skip further processing. if ( ! function_exists( 'pods_field' ) ) return ''; if ( null === $args ) { // In the loop. This means we are going through posts. $post_id = get_the_ID(); // Check if post is a product if (get_post_type($post_id) == 'products') { // Initialize the SEO title as the post (product) title $title = get_the_title($post_id); // Grabs the subtitle meta field from the product // $subtitle = get_post_meta($post_id, key:"subtitle", single:true); $subtitle = get_post_meta($post_id, "subtitle", true); // Check whether product has a featured brand by seeing if // "By: " is included in subtitle or features $brand_pos = strpos($subtitle, "By: "); // This conditional will be entered if a brand was found if ($brand_pos) { // brand_name_idx is the starting index of the brand name $brand_name_idx = $brand_pos + 4; // move brand_name_idx to the ending index of the brand name while ($brand_name_idx < strlen($subtitle)) { if ($subtitle[$brand_name_idx] == '|') { // Get rid of space (' ') before pipe seperator $brand_name_idx -= 1; break; } $brand_name_idx += 1; } $brand_name_length = $brand_name_idx - $brand_pos; // substr() function has args: (string, start_pos, length) $brand_name = substr($subtitle, $brand_pos, $brand_name_length); // Append brand name to title $title = $title . ' ' . $brand_name; } } else { // Not a product return ''; } } return $title; }, 10, 2 );
The problem is I’ve activated the snippet and the titles are not being set.
I can’t figure out when the ‘the_seo_framework_custom_field_description’ hook is triggered. That’s made it difficult for me to test and debug the code. Can you share more detail on how the hook and filter works?
- The topic ‘Auto-Generating Meta Titles with PODS Custom Fields’ is closed to new replies.