0

I have a function that contains a wpdb query. It's supposed to get the image alt text, based on the url of the image. This is the function:

// retrieves the attachment alt from the file URL
function get_image_alt_from_url($image_url) {
  global $wpdb;
  $id = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
  if ($id) {
    return get_post_meta($id[0], '_wp_attachment_image_alt', TRUE);
  }
  return false;
}

It used to work, but now it returns false every time. Upon debugging the function, I found that the $id variable is null.

I wonder if this has anything to do with upgrading PHP to 8.0, and if so, how to get around it. It still works on my local site, so I doubt it's related to a wp core update.

WordPress: 6.2.2 PHP: 8.0 Hosted on Kinsta

Thank you so much for your help,

Jill

5
  • WPDB::get_col() should always return an array, not null, even if you pass it null as a query. Where in the code snippet were you testing $id and finding it null?
    – Pat J
    May 24 at 22:03
  • 2
    Even if the issue gets fixed, it's still not best practice. Is there a reason you're interacting with the DB directly? It's harmless now, but it's good practice to always look for a better solution if the one you have is interacting with the database directly. You can get the picture ID from it's URL via $id = attachment_url_to_postid($image_url);. Once you have that, you can get it's alt via $alt = get_post_meta($id, '_wp_attachment_image_alt', TRUE);
    – alex
    May 24 at 23:57
  • 1
    @alex That's a good point, and it might be worth making it into an answer rather than a comment.
    – Pat J
    May 25 at 3:31
  • 1
    @PatJ Thank you, I was thinking I'm not actually solving the problem, just suggesting a different approach, but I added an answer where I tried to do both.
    – alex
    May 25 at 13:13
  • 1
    Sometimes the answer is literally "Do it a different way".
    – Pat J
    May 25 at 13:54

1 Answer 1

2

You should only interact with the DB via WP, not directly. It's not a big deal in your case because you just want to see a value, but you'll be more aligned with the best practices if you go in trying to solve a problem with $wpdb as a last resort. In your case, you can try this :

$id = attachment_url_to_postid($image_url);
$alt = get_post_meta($id, '_wp_attachment_image_alt', TRUE);

We'll first get the Post ID from the attachment URL, and then find the alt from that ID. This will do what you want without $wpdb.

That said, let's try and debug your original problem too!

As @PatJ said, WPDB::get_col() never returns null, so something's wrong. Turn on your debug like this :

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

The first line enables debugging mode, the second line ensures that debug messages are logged to a wp-content/debug.log file, and the third line prevents debug messages from being displayed on the site.

Run it again. Was an error logged? If not, run this :

echo $wpdb->last_error;

Also, after your get_col line, print the last query using the $wpdb->last_query command. This can help you make sure the query is as expected :

echo $wpdb->last_query;

Of course, check the image URL too - maybe it's not correct, or it's not in the database. It shouldn't return null, but worth checking.

Also, it's possible that the issue is not with your $wpdb->get_col() line but the get_post_meta() line. You can debug this by checking what it returns when you give it a known post ID and '_wp_attachment_image_alt' as the meta key.

1
  • Thanks so much for the thorough reply. This is actually what I ended up doing (I was rushed to fix this), but I appreciate the addition of the debugging tips. At some point I'll have to roll everything back and follow your debugging instructions, so I can follow up on why it didn't work... 54 mins ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.