rankmath / seo-by-rank-math

Rank Math is a revolutionary WordPress SEO Plugin that combines the features of many SEO tools and lets you multiply your traffic in the easiest way possible :bulb: :chart_with_upwards_trend: →
https://rankmath.com
107 stars 52 forks source link

Wrong featured image title/alt atribute value when %title% var is used #25

Closed dawnbirth closed 3 years ago

dawnbirth commented 3 years ago

Describe the bug

When the "Add missing TITLE attributes" or "Add missing ALT attributes" is on and the format is set to "%title%", the value of the attributes for the featured images sometimes holds a wrong value. This happens when the posts are displayed using a custom query and the loop doesn't depend on setup_postdata() to set the global $post to the current post in the custom loop.

To Reproduce Steps to reproduce the behavior:

  1. Go to Rank Math "General Settings" and open "Images" Tab
  2. Turn the "Add missing TITLE attributes" on and set the format to "%title%"
  3. Create a custom posts query inside another posts archive e.g 'category archive page', may be a loop like this:
    $some_posts = get_posts([
    'post_type' => 'post',
    'posts_per_page' => 5,
    'no_found_rows' => true,
    ]);
  4. Loop through the custom query posts and print the featured image without using "setup_postdata()", Like this may be:
    foreach($some_posts as $some_post) {
    echo '<div>';
    echo get_the_title($some_post) . '<br>';
    echo get_the_post_thumbnail($some_post);
    echo '</div>';
    }
  5. Check the title attributes for the displayed featured images, they hold a value different from the displayed post title.

Expected behavior The automatically generated ALT/TITLE attribute value for the featured image should be the same as the post title being displayed when the format is set to "%title%" from Rank Math image settings.

Suggested code Edit

In this file: includes/modules/image-seo/class-add-attributes.php

I think the callback used by Rank Math on the filter "post_thumbnail_html" should use a second attribute "$post_id" to get the current displayed post instead of depending on the current global $post.