Skip to content

Instantly share code, notes, and snippets.

@sybrew
Created August 6, 2024 17:09
Show Gist options
  • Save sybrew/e2813126da0d66a6f16c74b74ee7b117 to your computer and use it in GitHub Desktop.
Save sybrew/e2813126da0d66a6f16c74b74ee7b117 to your computer and use it in GitHub Desktop.
Fetch the property description from Easy Property Listings and use it as a custom description excerpt for The SEO Framework.
<?php
// Don't include the PHP opening tag if PHP is already opened in the script.
add_filter( 'the_seo_framework_description_excerpt', 'my_custom_meta_description_excerpt', 10, 2 );
/**
* @param string $excerpt The excerpt to use.
* @param array|null $args The query arguments. Contains 'id', 'tax', 'pta', and 'uid'.
* Is null when the query is auto-determined.
* @return string The adjusted excerpt.
*/
function my_custom_meta_description_excerpt( $excerpt, $args ) {
if ( isset( $args ) ) {
// Admin area
switch ( The_SEO_Framework\get_query_type_from_args( $args ) ) {
case 'single':
$post_id = $args['id'];
}
} else {
// Front-end
$tsfquery = tsf()->query();
if ( $tsfquery->is_singular() )
$post_id = $tsfquery->get_the_real_id();
}
if ( isset( $post_id ) && 'rental' === get_post_type( $post_id ) ) {
// Fetch the custom field value using EPL_Property_Meta
$property_meta = new EPL_Property_Meta( $post_id );
$property_description = (string) $property_meta->get_property_meta( 'property_description' );
if ( strlen( $property_description ) ) {
$excerpt = The_SEO_Framework\coalesce_strlen( tsf()->format()->html()->extract_content( $property_description ) )
?? $excerpt;
}
}
return $excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment