I'm trying to access a plugin's database from functions.php. With the code below, there are no errors, but there is no data echoing either. By looking around the web there are so many answers for so many different circumstances and none helped me apply any fixes to this particular code.
function show_review_count() {
global $wpdb;
$dbtable = 'wpcreviews';
$pageID = 4745;
$row = $wpdb->get_results("SELECT COUNT(*) AS `total` FROM `$dbtable` WHERE `page_id`=$pageID AND `status`=1");
echo $row[0]->total;
}
You're missing $wpdb's table prefix:
$dbtable = $wpdb->prefix . 'wpcreviews';
Related
I'm working on a project where I upload a file and use its path in a shortcode. Right now I've hard-coded the post's ID into my code but I want to make it dynamic so that new posts automatically get the correct shortcode.
<?php
global $wpdb;
$thepost = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->postmeta WHERE post_id = 5574" ) );
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $thepost->meta_value . '"]');
?>
Going off of what #Damocles said, you are really setting yourself up for trouble with your current path. Instead, there are built-in WordPress functions that abstract away the database and utilize caching that you are strongly encouraged to use.
// Get the current WordPress post
$the_post = get_post();
//If we have a value (there are cases where this will be empty)
if($the_post){
// Get the value from the post meta table by the current post's ID
$the_post_meta = get_post_meta($the_post->ID, 'YOUR_META_KEY_HERE', true);
// Double-check that we have a value
if($the_post_meta) {
// Finally, echo the shortcode
echo do_shortcode ('[sgpx gpx="'.'/wp-content/uploads/' . $the_post_meta . '"]');
}
}
There are several ways to get the current post however get_post() is the most common. If you are in a custom loop, however, you might need to adjust accordingly.
To access the meta, use get_post_meta() which includes some optimizations including use a cache instead of the database.
Although there are definitely exceptions, generally speaking, if you are working with WordPress and you find yourself writing SQL statements, there is almost always a better, safer, faster, etc. way to do it using core functions.
I followed the syntax the best I could to create a shortcode on execution, I got the wsod. Once removed, all was well. But I don't know what is wrong with my code. This code sits inside 'My Custom Functions', a plugin for wp.
In researching how to write a custom shortcode, I discovered instructions here: https://torquemag.io/2017/06/custom-shortcode/ My expertise is in mysql and use mostly plugins in our wordpress website. I am very limited with coding.
function last_updated_shortcode {
$last_updated = $wpdb->get_results( "SELECT MAX(process_time) FROM
qgotv.last_updated");
return $last_updated;
}
add_shortcode( 'last_updated', 'last_updated_shortcode' );
This shortcode should retrieve a max(datetime value) from a db table so it can be displayed on a page. The query works. The qgotv db is separate from the wordpress db but can be accessed through wp.
Two issues I can see, one is that you have a syntax error in your function. When defining a function in PHP, you need to include the arguments parenthesis: function my_function(){ /* Do Stuff */ }. Also, you probably need to reference the $wpdb with the global keyword.
You can read up a bit on the $wpdb class as well as creating your own functions.
This should get you sorted out:
add_shortcode( 'last_updated', 'last_updated_shortcode' );
function last_updated_shortcode(){
global $wpdb;
$last_updated = $wpdb->get_results( "SELECT MAX(process_time) FROM qgotv.last_updated");
return $last_updated;
}
I am working on a wordpress site with a custom theme, and I cannot figure out how to get $wpdb->prepare($query, $args) to work.
The Problem
I have a function in wp-content/themes/mytheme/php/functions.php which runs $wpdb->prepare($query, $args) to protect the query from SQL injection before executing the query and inserting some new data. However when I run this function, I get an error that reads "Call to a member function prepare() on null" on the line where $wpdb-prepare() is run. I did some googling and found that this means that $wpdb had not been defined.
What I tried
As per other threads I found online, I tried defining
global $wpdb
both inside my function and at the top of functions.php. When that didn't work I tried putting
include_one('/wp-includes/wp-db.php')
at the top of functions.php, but still nothing.
Does anyone have any other ideas about what I could try?
It's calling to the function just fine, but it's finding no valid input (null). What are you using to define your table name? I've had it before where a $wpdb won't run unless I've predefined it like this:
global $wpdb;
$table = $wpdb->prefix . "table_name";
$sql = $wpdb->prepare( "SELECT * FROM {$table} ORDER BY something DESC");
$result = $wpdb->get_results( $sql , ARRAY_A );
i need some help. its basic coding but the code still wont display my data stored.
here is my code: if someone can help me and explain. i've read the documentation and i followed it but it wont display my data. the code wont display my data stored in wp_options. is there something i am missing?
<?php
global $wpdb;
$result = $wpdb->get_results("
SELECT * FROM wp_options WHERE option_id = '262'
", ARRAY_A);
print_r($result);
}
?>
it just displays a blank screen.
I have read some documentation and comparable questions from others about my issue with wpdb, but I've seen very different answers so I'm slightly confused.
What I'm trying to do is get custom meta data from (eventually) about 250 pages in Wordpress. The wpdb request will be in header.php, and the code has to build an array which will serve as input for a Google Map. The custom meta fields are called _ytF_f_name, _ytF_f_lat and _ytF_f_lng (don't ask me why :-)).
So the final output should be: [name1,lat1,lng1], [name2,lat2,lng2], etc.
I'm using a custom table for Wordpress, which has the prefix yt_ (which is defined in wp-config.php). The custom meta is built with WPAlchemy. I have checked the database and the meta data is there (in the postmeta table).
After combining several things, this is what I have now:
<?php
global $wpdb;
$querystr = "SELECT ".$wpdb->prefix."postmeta._ytF_f_name, ".$wpdb->prefix."postmeta._ytF_f_lat, ".$wpdb->prefix."postmeta._ytF_f_lng FROM ".$wpdb->prefix."postmeta WHERE post_type='page'";
$vars = $wpdb->get_results($querystr);
foreach ($vars as $var) {
echo '[' . $var->_ytF_f_name . ',' . $var->_ytF_f_lat . ',' . $var->_ytF_f_lat . '],';
}
?>
The output is blank, so what am I missing here?
Another question related to this is; I've read something about 'prepare' to protect against sql injections.
Is the correct use of the prepare class to change
$vars = $wpdb->get_results($querystr);
into:
$vars = $wpdb->get_results($wpdb->prepare($querystr));
?
About your problem, I don't see the error, so verify if you have SQL errors with $wpdb->show_errors(); before your query (doc here).
The prepare method protects you against SQL injections.
As the doc said, it works like the following :
$wpdb->query(
$wpdb->prepare(
"INSERT INTO ___
( attr1, attr2 )
VALUES ( %s, %d )",
$valueOfAttr1,
$valueOfAttr2
)
);
Of course, you must change the types and values to your needs.