Custom WordPress Table wpdb

First off, you should not be handing an untrusted input (in this case, $_GET[‘id’] to your database. Always SQL-escape the query and validate/sanitize the data. (In the code snippet below, it’s SQL-escaped using $wpdb->prepare() for escaping and int typecasting to sanitize to integer value). Secondly, the $wpdb object provides more than just the query() function. … Read more

Using WPDB->Insert()

Short answer, no. WPDB is a class that’s been around for a while (back when WP used to support PHP 4), and lacks a lot of features PHP5 drivers offer, including parameter binding. As you mentioned, wpdb::insert is the closest you can get to your original code: $wpdb->insert( ‘importtest’, array( ‘id’ => $ID, ‘area’ => … Read more

WPDB for post count on post

The problem you are having is that get_queried_object returns results based on the query that was run. On author.php, the queried object was an author. On single.php, page.php, or any custom post type template, the queried object will be the post, not the author. This means that if you want to use $wp_query->get_queried_object();, you will … Read more

$wpdb->query can’t insert data

The following code will properly store data in wp_fafa: $wpdb->insert( $wpdb->prefix . ‘fafa’, array( ‘title’ => trim($row->item(0)->nodeValue), ‘liveprice’ => trim($row->item(2)->nodeValue), ‘changing’ => trim($row->item(4)->nodeValue), ‘lowest’ => trim($row->item(6)->nodeValue), ‘topest’ => trim($row->item(8)->nodeValue), ‘time’ => trim($row->item(10)->nodeValue) ), array( ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’, ‘%s’ ) );

Ajax insert or update data

I’m not entirely sure you want to call exit at the end of that function, since exit terminates all further PHP execution. This is likely the cause of your troubles. http://php.net/manual/en/function.exit.php That said, you can always test for the result of your queries by setting a variable to be the result, and then sending that … Read more

$wpdb returns duplicate posts

Nevermind, I solved it on my own. While reading on the WordPress Codex for $wpdb, I saw that if I use OBJECT_K it will remove the duplicates and that solved my problem. If it helps anyone, here is my working code: function latest_posts_after_last_login( $atts ) { global $wpdb, $current_user; $atts = shortcode_atts( array( ‘post_type’ => … Read more

meta_value timestamp older than now

<?php $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘event’, ‘meta_query’ => array( array( ‘key’ => ‘event_date_end_timestamp’, ‘value’ => time(), ‘type’ => ‘numeric’, ‘compare’ => ‘<‘ ) ) ); $programs = new WP_Query($args); ?> <?php while($programs->have_posts()): $programs->the_post(); ?> <h1><?php the_title(); ?></h1> <?php endwhile; ?>

Problem in using wpdb

This error: Fatal error: Call to a member function insert() on a non-object Means that $wpdb isn’t instantiated so you are running your code too early or out of WordPress context– for example, in an exterior PHP file accessed directly, which is what I suspect (Poorly conceived AJAX request perhaps?). As mentioned, the code has … Read more

Custom Query – Based on user input

While this is primarily a SQL question, there are WordPress components and Core has no efficient mechanism to pull this data that I am aware of. This has long been a problem in Core in my opinion. So: global $wpdb; // group name key $meta_key = ‘_create_new_group’; if (!empty($_REQUEST[‘q’])) { // user input $search = … Read more

Can’t run database query

Here issue with the column name. You are using column key in the query which is the default keyword/index in mysql. For resolving this kind of issue just use the “Grave accent(`)’ symbol in the query for the column name, No need to change the column name. So in your case the right query is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)