Not able to retrieve image id from sibling site within a switch_to_blog

Here is how I am trying to grab the image id for 'white_logo'.

switch_to_blog($site-blog_id);
echo attachment_url_to_postid( get_theme_mod('white_logo'));
restore_current_blog();

The echo is printing '0'. It seems that when I use 'attachment_url_to_postid' for grabbing IDs of images that are within the current site's media library that it works. But when I try to grab IDs of images within other sites media libraries I'm not getting the desired response. Is there an alternative way to achieve this? Thank you!

EDIT NOTE: I should add that I do know the 'switch_to_blog' is working properly as I am able to get the image url with 'get_theme_mod()'. I'm just unable to retrieve the ID with 'attachment_url_to_postid()'.

Topic switch-to-blog images Wordpress

Category Web


Just keep in mind this is only going to get you the image ID, but it should do the trick... does in my circumstance at least.

// switch to the correct blog
switch_to_blog( $site -> blog_id );

// pull in our db global
global $wpdb;

// get the image ID
$_image_id = $wpdb -> get_col( $wpdb -> prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", get_theme_mod( 'white_logo' ) ) );

// switch back to the original blog
restore_current_blog( );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.