Copy meta description to subtitle and excerpt?

#637740
  • Resolved Matthew Cannata
    Rank Math free

    Hello, looking to see if there is a way to automatically copy the meta description to the subtitle and excerpt fields in WordPress. Please let me know!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math support and we apologize for the inconvenience.

    There is no built-in way to automatically copy the meta description to the subtitle and excerpt fields in WordPress. However, you can use a custom PHP code or a plugin to achieve this. For example, you can create a Custom Plugin with this code:

    <?php
    /*
        Plugin Name: Rank Math Meta Description to Excerpt
        Plugin URI: https://support.rankmath.com/ticket/copy-meta-description-to-subtitle-and-excerpt-2/
        Description: Custom Plugin to copy Rank Math Meta Description to Excerpt
        Author: Rank Math Support
    
    */
    register_activation_hook( __FILE__, 'rm_to_excerpt' );
    
    function rm_to_excerpt()
    {   
        $args = array( 
            'post_type'   => 'post'
        ,   'numberposts' => -1
        );
        $posts = get_posts( $args );
        foreach ( $posts as $post )
        {
            if( '' == $post->post_excerpt )
            {
                $rm = get_post_meta( $post->ID, 'rank_math_description' ,true);
                if( '' != $rm )
                {
                    $po = array();
                    $po = get_post( $post->ID, 'ARRAY_A' );
                    $po['post_excerpt'] = $rm;
                    wp_update_post($po);
                }
            }
        }   
    }

    To create a folder in /wp-content/plugins and add the above code to a PHP file in the folder, then visit your WordPress dashboard to activate the plugin.

    This custom plugin code would iterate through your post and update every post without an excerpt with the post’s Rank Math meta description.

    We hope this helps you resolve the issue. If you have any other questions or concerns regarding Rank Math, please feel free to contact us again. We are always happy to help.

    Thank you for choosing Rank Math!

    Hello, I did as you said but it did not work. Also, does this copy it over to subtitle as well (TagDiv Composer/Newspaper Theme).

    The folder I created within plugins was called metacopy. The file name was pluginrankmath.php. Plugin was successfully activated.

    Here’s the code as you stated.

    <?php
    /*
        Plugin Name: Rank Math Meta Description to Excerpt
        Plugin URI: https://support.rankmath.com/ticket/copy-meta-description-to-subtitle-and-excerpt-2/
        Description: Custom Plugin to copy Rank Math Meta Description to Excerpt
        Author: Rank Math Support
    
    */
    register_activation_hook( __pluginrankmath.php__, 'rm_to_excerpt' );
    
    function rm_to_excerpt()
    {   
        $args = array( 
            'post_type'   => 'post'
        ,   'numberposts' => -1
        );
        $posts = get_posts( $args );
        foreach ( $posts as $post )
        {
            if( '' == $post->post_excerpt )
            {
                $rm = get_post_meta( $post->ID, 'rank_math_description' ,true);
                if( '' != $rm )
                {
                    $po = array();
                    $po = get_post( $post->ID, 'ARRAY_A' );
                    $po['post_excerpt'] = $rm;
                    wp_update_post($po);
                }
            }
        }   
    }

    Hello,

    That code will only work on standard post excerpts. It will not work on other custom meta fields. As for your subtitle, can you please confirm if this is also a custom field?

    Looking forward to helping you with this one.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Copy meta description to subtitle and excerpt?’ is closed to new replies.