How to get meta description by post id?

#655184
  • Resolved superpatch
    Rank Math pro
    SEO Course

    I have a loop of posts and trying to get the auto generated meta description using the following functions.. Everything is blank

    
    if( have_posts() ){
    	while ( have_posts() ) : the_post();
    	?>
    	<a>" class="post-listing-item" title="<?php the_title(); ?>">
    		<?php the_post_thumbnail('full'); ?>
    		<h3 nodrop><?php the_title(); ?></h3>
    		<?php
    			$d1 = RankMath\Paper\Paper::get()->get_description();
    			$d2 = RankMath\Post::get_meta( 'description', get_the_ID() );
    			$d3 = RankMath\Post::get_meta( 'rank_math_description', get_the_ID() );
    			
    			echo $d1;
    			echo $d2;
    			echo $d3;
    		?>
    	</a>
    	<?php
    	endwhile;
    	wp_reset_postdata();
    }
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused.

    Please replace the 3 lines with below instead and see if that works for you:

    global $post;
    $d1 = RankMath\Paper\Paper::get_from_options( "pt_{$post->post_type}_description", $post, '%excerpt%' );
    $d2 = RankMath\Post::get_meta( 'description', $post->ID );
    $d3 = RankMath\Post::get_meta( 'rank_math_description', $post->ID );

    Let us know how that goes. Looking forward to helping you.

    superpatch
    Rank Math pro
    SEO Course

    Hello.. this is what I observed.

    $d3 is always blank. Not sure how to get it to work.
    $d2 requires adding description in the RankMath Snippet.
    $d1 is the excerpt of the post that is auto generated by WordPress.

    I’m trying to replicate how RankMath generates the description for Social Media tags like og:description where it automaticaly adds description without adding anything to the RankMath snippet.

    Can you help?

    Hello,

    $d3

    Can you please confirm if you intend to retrieve the individual post meta description and have the global option of the meta description as your default/fallback?

    Please try the code below and see if it works for you:

    RankMath\Post::get_meta( 'description', $post->ID, RankMath\Paper\Paper::get_from_options( "pt_{$post->post_type}_description", $post, '%excerpt%' ) );

    We have tested this on our local setup with success and this code should work on your website as well.

    $d2

    This is quite expected. If the description meta is blank, then this will render blank. What you can do here is to add or create a fallback value for this variable.

    $d1

    Can you please share a screenshot of the current meta description of the post that is shown on the Rank Math SEO metabox?

    We really look forward to hearing back from you. Thank you.

    superpatch
    Rank Math pro
    SEO Course

    If you look at the below screenshot, you can see if the RankMath snippet is blank, there is a default description added my RankMath here. How can I get this default description?

    Sample screenshot

    Hello,

    That is expected since the meta description for that individual post is technically “blank”, and our plugin’s behavior is to derive from the global settings (Titles & Meta).

    In your code, it will only see the blank, not the the global settings so you need to create a logic that will have a fallback.

    Hope that helps and please do not hesitate to let us know if you need my assistance with anything else.

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

You must be logged in to reply to this ticket.