• Resolved Bjarne Oldrup

    (@oldrup)


    Hey Sybre

    You answered a similar question before regarding using a ACF field for meta description and I’m trying to modify the code snippet to pull meta description and title from Pods Framework fields, but I can’t get it to work.

    The stage:
    I’ve got a custom post type called Materials with the following fields:
    Title (core)
    – Featured image (core)
    Variants (custom plain text field, 2-3 words)
    Note (custom plain text field, max 150 characters)

    I’d like my Meta Title to be a concatenation of the core post Title + Variants, unless a custom title is entered in the TSF meta box on the post edit screen, of course.

    I’d like my Meta Description to my Note field, unless a custom description is entered in TSF meta box.

    I am pretty sure that is achievable in TCF and Pods, but as said, I’m having a hard time getting it to work, and even if I did, I’d like to be sure best practices are followed and the code is efficient and robust – not crashing if, say, I disable TSF or Pods for maintenance.

    Can you lead me in the right direction?

    Kind regards
    Bjarne

    The page I need help with: [log in to see the link]

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

    (@cybr)

    Hi Bjarne,

    What’s the code you have now?

    I have no experience with Pods, but it looks like you can simply change get_field to pods_field. I added a new index for $args['pta'] in TSF v4.2.0, but that shouldn’t cause real issues. Accounting for all I mentioned, this should do for the description:

    add_filter( 'the_seo_framework_custom_field_description', function( $desc, $args ) {
    
    	// A custom description is found for TSF already, let's skip further processing.
    	if ( $desc ) return $desc;
    
    	// If Pods is deactivated, skip further processing.
    	if ( ! function_exists( 'pods_field' ) ) return '';
    
    	if ( null === $args ) {
    		// In the loop.
    		$desc = pods_field( 'note' );
    	} else {
    		// Out the loop.
    		if ( ! empty( $args['pta'] ) ) {
    			// Post Type Archive query... do something with get_post_type_object()?
    		} elseif ( ! empty( $args['taxonomy'] ) ) {
    			// Taxonomy query... do something with 'taxonomy' and 'id'?
    		} else {
    			// Singular query.
    			$desc = pods_field( 'note', $args['id'] ) ?: '';
    		}
    	}
    
    	return $desc;
    }, 10, 2 );

    For the title, the code is practically the same, but then the filter is the_seo_framework_title_from_custom_field, and every $desc could better be called $title. Let me know if these pointers work, share your work (if any thus far) if you still need assistance, and I’ll see what I can do.

    Cheers ??

    Thread Starter Bjarne Oldrup

    (@oldrup)

    Hey!

    Thank you for your pointers.

    Something works, but I’m not quite there yet.

    Code:
    $desc = pods_field("note");

    Results in:
    <meta name="description" content="Array">

    While:
    $desc = 'This is just a dummy text';

    Results in:
    <meta name="description" content="This is just a dummy text">

    I think I have to, like, “locate the pod” by the ID, before I can fetch the fields, but, the official documentation is hard to grasp for a PHP newb.

    Also, the pods_field function takes four arguments.

    The “field” function might also be helpful and better documented, but the hardcoded id 123 in the example throws me off.

    I’ll do some more trial and error. If I find a solution, I’ll be sure to share it.

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Bjarne,

    Thanks for the update. I missed the 4th parameter: $single = false (again, I have no experience with Pods), which is why it returned an array. PHP converts an array to text as “Array” (and emits a warning).

    field() is a child method of pods(), which adds complexity to writing code with compatibility in mind, and I do not wish to burden you with that when it’s not required. It is why I went straight to the more straightforward public API function pods_field().

    This revision should be closer to an actual solution, where I set $single to true:

    add_filter( 'the_seo_framework_custom_field_description', function( $desc, $args ) {
    
    	// A custom description is found for TSF already; let's skip further processing.
    	if ( $desc ) return $desc;
    
    	// If Pods is deactivated, skip further processing.
    	if ( ! function_exists( 'pods_field' ) ) return '';
    
    	if ( null === $args ) {
    		// In the loop. Let Pods figure out the query, grab single.
    		$desc = pods_field( 'note', null, null, true );
    	} else {
    		// Out of the loop.
    		if ( ! empty( $args['pta'] ) ) {
    			// Post Type Archive query... do something with get_post_type_object()?
    		} elseif ( ! empty( $args['taxonomy'] ) ) {
    			// Taxonomy query... do something with 'taxonomy' and 'id'?
    		} else {
    			// Singular query. Grab single result.
    			$desc = pods_field( 'note', $args['id'], null, true ) ?: '';
    		}
    	}
    
    	return $desc;
    }, 10, 2 );
    Thread Starter Bjarne Oldrup

    (@oldrup)

    Thank you again for your support ??

    While the revised snippet still returned an array, the good Paul Clark from the Pods Framework Slack team, suggested using this function instead:

    $desc = get_post_meta( get_the_ID(), 'note', true );

    And that works.

    Also, as we already went live with the site, I learned that Google (and Bing) actually are much better at generating a description which actually relates to the search term. I guess that is another benefit of creating semantic meaningful and accessible HTML.

    So, I’ll leave that job to the search engines.

    I’m going to add some variants to the title field though, as I have a custom plain text field for that. Think Post Title: Fruits. Variants: Banana, Apple, Orange – to create a Post title like “Fruits (Banana, Apple, Orange)”.

    But that I think I can figure out, now I know how to fetch the custom fields from the post meta. Will be a fine little exercise for me to get started with PHP and the WordPress API.

    There are other interesting Pods fields to hook into, including relations; boolean fields, taxonomies etc. Pods Framework is a powerful tool for custom post types, and I actually prefer it over other premium tools even though I got my licenses, but the documentation can be difficult to grasp. I’ll build a small demo and post the code on my blog once done.

    Again, thanks alot! SEO Framework is fantastic.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Pulling a Pods Framework custom field for meta title and description?’ is closed to new replies.