Home / Admin / Fetch and Display AIOSEO Meta Data for a Specific Post
Duplicate Snippet

Embed Snippet on Your Site

Fetch and Display AIOSEO Meta Data for a Specific Post

This code snippet will fetch the AIOSEO Post Title, Meta Description, Meta Keywords, the Focus Keyphrase, and all Additional Keyphrases from a post using the Post ID.

<10
Code Preview
php
<?php
/**
 * WPCode Snippet: Display AIOSEO Meta Data for Post ID 323
 * 
 */
add_filter('the_content', function($content) {
    // Only show on single post/page and for post ID 323
    if (!is_singular() || get_the_ID() != 323) {
        return $content;
    }
    if (!function_exists('aioseo')) {
        return $content . '<div style="background: #ffebee; padding: 15px; margin: 20px 0; border-left: 4px solid #f44336;"><strong>Error:</strong> AIOSEO plugin is not active.</div>';
    }
    $post_id = 323;
    $post = get_post($post_id);
    
    if (!$post) {
        return $content . '<div style="background: #ffebee; padding: 15px; margin: 20px 0; border-left: 4px solid #f44336;"><strong>Error:</strong> Post not found.</div>';
    }
    $output = '<div style="background: #e3f2fd; padding: 20px; margin: 20px 0; border-left: 4px solid #2196f3; font-family: monospace; font-size: 14px;">';
    $output .= '<h3 style="margin-top: 0; color: #1976d2;">AIOSEO Meta Data for Post ID: ' . $post_id . '</h3>';
    // Get AIOSEO Post object for additional data
    $aioseo_post = AIOSEO\Plugin\Common\Models\Post::getPost($post_id);
    // Get meta keywords from meta field
    $meta_keywords = get_post_meta($post_id, '_aioseo_keywords', true);
    $meta_keywords_array = [];
    if (!empty($meta_keywords) && is_string($meta_keywords)) {
        $meta_keywords_array = array_map('trim', explode(',', $meta_keywords));
    }
    // Get focus keyphrase and additional keyphrases
    $focus_keyphrase = '';
    $additional_keyphrases = [];
    if (!empty($aioseo_post->keyphrases) && is_object($aioseo_post->keyphrases)) {
        $keyphrases = json_decode(wp_json_encode($aioseo_post->keyphrases), true);
        if (!empty($keyphrases['focus']['keyphrase'])) {
            $focus_keyphrase = trim($keyphrases['focus']['keyphrase']);
        }
        if (!empty($keyphrases['additional']) && is_array($keyphrases['additional'])) {
            foreach ($keyphrases['additional'] as $additional) {
                if (!empty($additional['keyphrase'])) {
                    $additional_keyphrases[] = trim($additional['keyphrase']);
                }
            }
        }
    }
    // Get title and description using proper AIOSEO methods (these will apply filters)
    $aioseo_title = aioseo()->meta->title->getPostTitle($post);
    $aioseo_description = aioseo()->meta->description->getPostDescription($post);
    // Output the data
    $output .= '<strong>Meta Title:</strong> ' . esc_html($aioseo_title) . '<br>';
    $output .= '<strong>Meta Description:</strong> ' . esc_html($aioseo_description) . '<br>';
    $output .= '<strong>Meta Keywords:</strong> ' . (!empty($meta_keywords_array) ? esc_html(implode(', ', $meta_keywords_array)) : 'Not set') . '<br>';
    $output .= '<strong>Focus Keyword:</strong> ' . ($focus_keyphrase ? esc_html($focus_keyphrase) : 'Not set') . '<br>';
    $output .= '<strong>Additional Keywords:</strong> ' . (!empty($additional_keyphrases) ? esc_html(implode(', ', $additional_keyphrases)) : 'None') . '<br>';
    $output .= '</div>';
    return $output . $content;
}, 5);

Comments

Add a Comment