$wpdb is null even after 'global $wpdb - wordpress

I have seen almost all links but still I am unable to solve my problem.
I am getting $wpdb as null.
I am checking it like this. I am doing this in single.php file
echo "<pre>";print_r($wpdb);"</pre>";
I have checked about following files. That all are loaded.
wp-config.php
wp-load.php
wp-includes/wp-db.php
Please help me.
EDIT
I want to execute custom query like this.
$entries = $wpdb->get_results( $wpdb->prepare( "SELECT forms.form_title, entries. *
FROM wp_visual_form_builder_forms AS forms
INNER JOIN wp_visual_form_builder_entries AS entries ON entries.form_id = forms.form_id" ) );
this is not working.

If you read the documentation, you will notice this passage:
Always use the global $wpdb variable. (Remember to globalize $wpdb before using it in any custom functions.)
It's not very clear but I think that means you cannot use $wpdb outside of a function. What I recommend you do is create a function in your theme's functions.php file, and call that function from the single.php file.

Related

Wordpress does not save Custom Field

I am working with several Custom Fields which are "allocated" to Custom Post Types. For some odd reason one Custom Field needs to be created over and over again. I cannot seem to find a solution that this specific Custom Field is saved onto my Wordpress Database for future use.
The Custom Field is called "video_url". Even if I rename it so "youtubelink" or whatsoever Wordpress does not save it. Any other newly created Custom Fields are saved by Wordpress but not that one that must hold the Youtube Link.
Any ideas? Is there something to "force" WP to keep that Custom Field?
NB: I am not using any Plugins to create Custom Fields.
I think you need use a php function to update or execute this sql in phpmyadmin
update `<your_prefix>_postmeta` pm set pm.meta_key = 'youtubelink' where pm.meta_key = 'video_url'
If you use $wpdb you can add this code to function.php in your theme
global $wpdb;
echo $sql = "update `{$wpdb->prefix}postmeta` pm set pm.meta_key = 'youtubelink' where pm.meta_key = 'video_url'";
$wpdb->query($sql);
exit;
i added exit to make sure this code is executed. You reload page to run this script and then comment them to check. If nothing updated you use this code
global $wpdb,$prefix;
echo $sql = "update `{$prefix}postmeta` pm set pm.meta_key = 'youtubelink' where pm.meta_key = 'video_url'";
$wpdb->query($sql);
exit;
this is code used in old wordpress version. You reload page to run script, comment code to check again...
I solved this problem by increasing the Post Meta in my functions.php
Looks like Wordpress has a postmeta limit # 30. With the function below you can increase it to higher values.
Works like a boss
/* Increase Postmeta Limit.*/
add_filter( 'postmeta_form_limit', function( $limit ) {
return 100;
} );

Wordpress and Woocmmerce - how to access settings from theme?

Im trying to pull the settings from Woocommerce admin. I found this:
http://oik-plugins.eu/woocommerce-a2z/oik_api/woocommerce_settings_get_option/
$string = woocommerce_settings_get_option( $option_name, $default );
It looks to be a public function but I cannot access from my theme files. It just gives me Fatal error: Call to undefined. Anyone have any idea how you can access the setting from the theme?
I'm trying to get 'woocommerce_frontend_css_primary', $colors['primary'] so can tie them into the rest of the theme. Woocommerce currently just write the values directly to .less file.
Woocommerce docs are a bit misleading, but it turns out there is another function called get_option... as long as you know the name of the option you can use. EG. get array of front end colors:
$woo_styles = get_option( 'woocommerce_frontend_css_colors' );

Does query_posts have an effect on get_the_category?

I am having trouble debugging a situation, and I think some better background information on how these systems work would be very helpful. I know that the use of the function query_posts() is strongly discouraged. But let's just assume that there is nothing I can do to remove it from the code. Specifically the query is changed to pull posts of a post_type (a post_type specific to the theme I am using). This is what the query looks like after it has been changed:
posts_per_page=10&paged=0&post_type=project
Then the loop begins and it can successfully grab the titles for each post. But when I call get_the_category() it returns an empty array, even though all of the posts have categories. I verified that it was an empty array with var_dump.
I am do not have a super strong understanding of how these systems work, so the emphasis on not using query_posts has me worried. Is there any possible interaction between query_posts and get_the_category() that could cause it to not work correctly?
Why not using WP_Query()? Im not sure it is cause by query_posts, but query_posts() alters Wordpress Main Loop (global $wp_query).
You can use the WP_Query method like this, by replacing with your query_posts loop
$newLoop = new WP_Query('posts_per_page=10&paged=0&post_type=project');
if ( $newLoop->has_posts() ) {
while ( $newLoop->has_posts() ) { $newLoop->the_post();
the_title(); // Your title
var_dump ( get_the_category() ); // this should not be empty if category assigned to current post
}
}
If you still want to go with query_posts try one of the methods below:
Try passing post id manually:
get_the_category( get_the_ID() ); // Must be in loop
Or try adding this:
// after endwhile of query_posts
wp_reset_query();

wp custom plugin db insert data

I am trying to develop a custom plugin in word-press. I have created a form which calls the following script:
<?php
global $wpdb;
$sql="insert into wp_wbp_unpublished values(null,'".$_POST['bibkey']."',
'".$_POST['author']."','".$_POST['title']."','".$_POST['date']."','".$_POST['note']."',
'".$_POST['keywords']."','nothing');";
echo $sql;
//$wpdb->show_errors();
$wpdb->insert("wp_wbp_unpublished",array('uid'=>null,'pid'=>$_POST['bibkey'],
'author'=>$_POST['author'],'title'=>$_POST['title'],'year'=>$_POST['date'],
'note'=>$_POST['note'],'keyword'=> $_POST['keywords'],'abstract'=>"null"));
$wpdb->query($sql);
//$wpdb->print_error();
?>
As you can see, I have tried two different ways to insert data into my db but nothing worked.
The sql string is correct, I have checked it. But something wrong with the insert()/ query() commands. Any suggestions of what is wrong?
Honestly, I do not see anything wrong with your code. At the end of the day I always blame Wordpress for the fact that it sucks asshoe. I am having the same issue, except only after the user logs into the index file and tries to make changes to db. Why hasn't anyone answered this question? Wtf
You have to remove semicolon(); in your custom or first insert query
$sql="insert into wp_wbp_unpublished values(null,'".$_POST['bibkey']."',
'".$_POST['author']."','".$_POST['title']."','".$_POST['date']."','".$_POST['note']."',
'".$_POST['keywords']."','nothing');";
And other thing is when you make plugin, then you have to put "upgrade.php" file in you function...
I hope it will work.
I have used the following solution, which seems to be the best:
$wpdb->insert( $table, $data, $format );
For instance:
$wpdb->insert(
$prefix.'wp_wbp_unpublished',
array('bibkey'=>$_POST['bibkey'],'author'=>$author),
array('%s','%s')
);
More information: https://codex.wordpress.org/Class_Reference/wpdb#INSERT_row

Wordpress(wpdb class) and mysql stored procedures

$query = "CALL ..........";
$result = $wpdb->get_results($query);
print_r($results);
Is returning an empty array, how do you run stored procedures from the $wpdb class?
I'm not 100% sure since I've not worked with stored procs in MySQL yet (although I have coded many a stored-proc for MS-SQL in the 90's), but try:
global $wpdb;
$wpdb->query('CALL ...');
print_r($wpdb->last_result);
Whatever you pass to $wpdb->query() gets passed to the PHP mysql_query() function so whatever works there will work with WordPress' $wpdb->query(). I do know this works:
global $wpdb;
$wpdb->query('SELECT * FROM wp_posts LIMIT 10');
print_r($wpdb->last_result);
If you want to read the source code for $wpdb->query() to further understand what is happening you can find it in /wp-includes/wp-db.php at lines 1057-1153 in v3.01.
Hope this helps.
-Mike
P.S. You might want to check out StackOverflow's sister site WordPress Answers next time you need help with WordPress. Lots of WordPress enthusiasts are on hand over there to answer your WordPress questions.

Resources