EDIT: Adding more background info.
I've coded a plugin that adds a meta box to the post page. When the post is published, the plugin will generate a random ID for the post, and insert the random ID and the post guid to a custom table in the database. After publishing, or upon editing, the plugin queries the custom table to retrieve the random ID for the post based on the post guid, and prints out the random ID in the meta box.
Everything works great until I change the post type in the add_meta_box function from 'post' to 'quilt'. The plugin will generate the random ID and save it and the post guid to the table, but presents this error in the meta box:
Notice: Trying to get property of non-object in /home/qultshow/public_html/wp-content/plugins/randomID/randomID.php on line 126
instead of the random ID. If I use
var_dump( $query );
$wpdb->print_error();
to try to debug, I see:
NULL
WordPress database error: [] SELECT * FROM wp_customtable WHERE qrci_guid = "http://testsite.com/?post_type=quilt&p=213"
If I copy the query from the Wordpress database error directly and run it in phpmyadmin, a row is returned as expected. So, the query is coded correctly, and the plugin works on the default post type of "post" but not on the custom post type of "quilt". The only change in the plugin is in this code:
add_meta_box( 'randomid_sectionid', 'Random ID', 'cd_meta_box_cb', 'quilt', 'normal', 'high' );
where 'post' was changed to 'quilt'.
Is there something odd or different about querying the database from a custom post type that I need to know about to make this work?
Notes: This plugin is not for usage by anyone but me on one website where I need this weird functionality. Also, this is a multi-site installation of wordpress if that makes any difference. I can't use post metadata for this since I need to be able to query this data without querying every postmeta table on the network, so it has to be a custom table in the database.
Here's the pertinent code that is not returning a row on custom post types, but does return a row on default post types. Maybe it's not the best written code, but it does work on default post types:
global $wpdb;
$wpdb->show_errors();
$sql = 'SELECT * FROM ' . $wpdb->base_prefix . 'customtable WHERE qrci_guid = "'. $post->guid . '"';
$query = $wpdb->get_row($sql);
$_quilt_id = $query->quilt_id;
var_dump( $query );
$wpdb->print_error();
And again, here are the errors on running:
Notice: Trying to get property of non-object in /home/qultshow/public_html/wp-content/plugins/metaboxplugin/metaboxplugin.php on line 126 somewebsite/?post_type=quilt&p=49
NULL
WordPress database error: [] SELECT * FROM wp_customtable WHERE qrci_guid = "http://testsite.com/?post_type=quilt&p=213"
I initially thought that there was an issue with the ampersand in the guid that the query searches on, because it was getting inserted into the custom table as &
instead of just an ampersand. I fixed that problem, and now it is inserted into the table correctly, as just an ampersand, so there shouldn't be any issue pulling a record out of the table. That being said, on default post types, the ampersand is not in the guid, and the code works great. Maybe that really is the issue, but I don't know how to fix it.
Can anyone help out with this?
$query
.SELECT * FROM wp_customtable WHERE qrci_guid = "somewebsite/post_type=quilt&p=49"