Trouble with SQL SELECT inside switch_to_blog()

I'm using switch_to_blog() and restore_current_blog() to perform some functions across a multi site install. One of the things I'm trying to do is select a very specific post using get_row(). My function looks like this:

$prefix = $wpdb-prefix;
$tbl = $prefix;
$product = $wpdb-get_row("SELECT * FROM {$tbl}_posts WHERE ID = 4253");
print_r($product); exit;

The post with ID 4253 definitely exists, and I know for sure that the prefix is correct, however print_r returns absolutely nothing, nor does echo $product-post_title;

Any ideas?

Topic get-row switch-to-blog multisite Wordpress

Category Web


Edit: You have an extra underscore in your table name, it becomes wp__posts in your case. Better use like how I have shown below :)


First, use table names like the following and try var_dump to see if it returns NULL. In case the Post ID doesn't exist, $product will be NULL, which only var_dump can show.

$product = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID = 4253"); var_dump( $product );

About

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