Auto Default focus keyphrase and Meta description on Youst SEO


/**
 * Set default focus keyphrase for all WooCommerce products.
 *
 * If the product title is longer than 60 characters it will be truncated.
 * If the (possibly truncated) title is less than 55 characters,
 * the product categories will be appended at the end.
 * If no categories are found, " - best sale" will be appended instead.
 *
 * If the product has subcategories, only the subcategory names are used.
 * Otherwise, the main category is used.
 */
function set_default_focus_keyphrase_for_woocommerce_products() {
    // Check if WooCommerce is active.
    if ( ! class_exists( 'WooCommerce' ) ) {
        return;
    }

    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 50, // Limit batch size.
        'paged'          => 1,  // Start from page 1.
    );

    $query = new WP_Query( $args );

    // Loop through each batch of products.
    while ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            // Get the product title.
            $product_title = get_the_title();

            // If the title is longer than 60 characters, truncate it.
            if ( strlen( $product_title ) > 60 ) {
                // Get a substring of the first 60 characters.
                $truncated = substr( $product_title, 0, 60 );
                
                // Find the last space in the substring to avoid cutting a word in half.
                $last_space = strrpos( $truncated, ' ' );
                if ( $last_space !== false ) {
                    $product_title = substr( $truncated, 0, $last_space );
                } else {
                    // If no space is found, use the 60-character substring.
                    $product_title = $truncated;
                }
            }

            // If the (possibly truncated) title is less than 55 characters,
            // attempt to append product categories or fall back to "- best sale".
            if ( strlen( $product_title ) < 55 ) {
                // Get product categories (using the 'product_cat' taxonomy).
                $categories = get_the_terms( get_the_ID(), 'product_cat' );
                
                if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {
                    // Filter to get only subcategories (where parent is not 0).
                    $sub_categories = array_filter( $categories, function( $cat ) {
                        return (int) $cat->parent !== 0;
                    } );
                    
                    if ( ! empty( $sub_categories ) ) {
                        // If subcategories exist, join all subcategory names.
                        $category_names = wp_list_pluck( $sub_categories, 'name' );
                        // Join the names with a comma and a space.
                        $categories_str = implode( ', ', $category_names );
                    } else {
                        // No subcategories found; use the main category.
                        // Filter to get only main categories (where parent equals 0).
                        $main_categories = array_filter( $categories, function( $cat ) {
                            return (int) $cat->parent === 0;
                        } );
                        // Use the first main category.
                        $first_main = reset( $main_categories );
                        $categories_str = $first_main->name;
                    }
                    $product_title .= ' - ' . $categories_str;
                } else {
                    // If no categories exist, append " - best sale".
                    $product_title .= ' - best sale';
                }
            }

            // Update the Yoast focus keyphrase meta with the (possibly modified) product title.
            update_post_meta( get_the_ID(), '_yoast_wpseo_focuskw', $product_title );
        }

        // Move to the next batch of products.
        $args['paged']++;
        $query = new WP_Query( $args );
    }

    // Reset post data.
    wp_reset_postdata();
}
add_action( 'init', 'set_default_focus_keyphrase_for_woocommerce_products' );

meta description auto as description

%%excerpt%% 

Leave a Comment

Your email address will not be published. Required fields are marked *