• Resolved nautical7419

    (@nautical7419)


    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?

Viewing 1 replies (of 1 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    Sorry for the delay; your support request was a bit too daunting for me to tackle immediately.

    The filter is only for overwriting TSF’s custom field. So, I recommend using the filter the_seo_framework_title_from_generation instead so you can still overwrite TSF’s filter.

    Sidenote: that filter won’t be reflected in the input field of TSF because it’s rewritten via JS whenever the page title changes. I still need to figure out how to resolve that best. Everything else, including the SEO Bar, will listen to that filter properly.

    Next, you mentioned the ‘product’ post type, but in the code, it says 'products'. If you use WooCommerce or EDD, TSF has an API function to accurately determine the product post type on the front-end: tsf()->query()->is_product().

    Moreover, TSF automatically clears duplicated spaces, so you need not worry about that.

    Lastly, I’m not entirely sure what you’re trying to do with the brand-fetching loop, but that should be better served via a regular expression. I made an assumption in mine based on your code, but if you have a couple of examples of subtitle fields, I can tweak the regex to get them with great accuracy and speed.

    With all that, I believe this will be an excellent starting point: https://gist.github.com/sybrew/7a0811464ea1ab825b1ebae2266012bb. Note that I expect you to be on TSF v5.0 or later — it will crash on earlier versions of TSF.

    I hope the filter makes more sense now that it’s tweaked according to your requirements.

Viewing 1 replies (of 1 total)
  • The topic ‘Auto-Generating Meta Titles with PODS Custom Fields’ is closed to new replies.