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.
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'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';
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.
I have added a field in wordpress posts table named "parent_location". How to get the value of this field in db front end? I have tried get_post_custom_values('parent_location', $post_id) function but it not working. Can any body help me out?
Thanks in advance, don't down vote
You added the field as a new column in wp_posts or added a new metadata entry to a single post? If its to a post then just do a get_post_meta(postID, 'parent_location', true); It its a new column into wp_posts, then should you not be able to just to a standard;
<?php
$my_id = 100;
$post = get_post($my_id);
$parent_loc = $post->parent_location;
?>
Not sure if that will work, I've not tried to get a custom column via that method before. If it doesn't, just roll your own SQL query; http://codex.wordpress.org/Class_Reference/wpdb
I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>