(There are a lot of related questions to this but none I can find with answers that have worked.)
get_var returns empty when I query a custom table from a custom plugin for multisite. What am I missing? (I have also tried get_row and get_results which also return empty.)
This is for a publish/unpublish button in the subsite admin using jquery/ajax. The publish function works, creates a new post in the subsite user's blog, and inserts the post_id, blog_id and uniquecode in the custom table. The unpublish function also works in so far as I am able to delete the correct row from the custom table.
What's not working is, within the unpublish function, before deleting the row from the custom table, I need to retrieve the post_id and delete the post from the subsite user's blog, but the get_var query for the post_id returns empty.
Here is the relevant portion of the unpublish function, showing the get_var query and delete correct row, plus print_r checks at bottom:
global $wpdb;
$customtable = $wpdb->$table_prefix.'customtable'; // works for $wpdb->delete but not $wpdb->get_var
$user_blog_id = get_current_blog_id(); // works
$uniquecode = $_POST[uniquecode]; // works via jquery-ajax, value obtained from external database
$post_id = $wpdb->get_var($wpdb->prepare('SELECT post_id FROM '.$customtable.' WHERE uniquecode = '.$uniquecode.' AND blog_id = '.$user_blog_id.'')); // not working, returns empty
wp_delete_post ($post_id, true); // $post_id = $wpdb->get_var is empty, preventing wp_delete_post from working
$wpdb->delete( $customtable, array // works - deletes row from $customtable
(
'blog_id'=>$user_blog_id,
'uniquecode'=>$uniquecode
),
array('%d','%s')
);
echo 'Post ID#';
print_r($post_id); // returns empty
?><br><?
echo 'Unique code #';
print_r($uniquecode); // returns correct value
?><br><?
echo 'Blog ID#';
print_r($user_blog_id); // returns correct value