using $wpdb to insert a form into a post

In the success part of your code, you can build an array representing a post, and use wp_insert_post as such : Example $mypost = array( ‘post_title’ => ‘My Title’, ‘post_type’ => ‘page’ //… add other fields according to your form ); $mypost_id = wp_insert_post( $mypost ); //Returns new post id on success Any field you … Read more

What is wrong with this wpdb update?

you’re creating the $data and $where values as strings, not arrays. this: $data = “array( ‘somecol’ => ‘”.$newstr.”‘ )”; $where = “array( ‘id’ => “.$defid.” )”; should be: $data = array( ‘somecol’ => $newstr ); $where = array( ‘id’ => $defid );

Confused by $wpdb->prepare

First, prepare() allows you to specify two different types of data: %s for String Types %d for Integer/Numeric Types The reason is simple: MySQL only knows two (and a half) data types: Strings and Numerics (and Date/Time). So this way you specify what type it is and make sure only that type gets trough. Still … Read more

WPDB – How to search a column in a table

What you are asking for should be straight SQL. Try adapting the following: function check_for_ip($ip) { global $wpdb; $sql = <<<SQL SELECT true FROM `wpgrating` WHERE `ip` = %s SQL; return (bool) $wpdb->get_var($wpdb->prepare($sql, $ip)); } In essence, we do a simple SELECT with the IP in question as in the WHERE clause. (Note that it’s … Read more

Get an array of meta_values for a user meta_key

You just need to add a DISTINCT to your SQL query, something like: $cities = $wpdb->get_col(“SELECT DISTINCT(meta_value) FROM $wpdb->usermeta WHERE meta_key = ‘my_cities_meta_key'” ); Alternatively, if you want to do it with php for some reason (if you want to know that a city is listed twice before displaying only unique entries) $cities = $wpdb->get_col(“SELECT … Read more

$wpdb select query by month, post type, and taxonomy term

You’ve not joined the wp_terms or $wpdb->terms table where WordPress stores the term names. So here is the updated code- $post_type_query = ” AND post_type=”” . $selected_post_type . “””; $posts_per_month = $wpdb->get_results( “SELECT *, DATE_FORMAT(post_date, ‘%Y-%m’) AS month, COUNT(ID) AS count FROM {$wpdb->posts} AS wposts LEFT JOIN {$wpdb->postmeta} AS wpostmeta ON (wposts.ID = wpostmeta.post_id) LEFT … Read more

How to get an array of user roles with or without a specific capability?

Try with this: function get_roles_that_cant($capability) { global $wp_roles; if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles(); $available_roles_names = $wp_roles->get_names();//we get all roles names $available_roles_capable = array(); foreach ($available_roles_names as $role_key => $role_name) { //we iterate all the names $role_object = get_role( $role_key );//we get the Role Object $array_of_capabilities = $role_object->capabilities;//we get the array … Read more

Foreach loop using $wpdb not results from rows

First of all, you should never concatenate SQL Query with any variables this way – it will cause SQL Injection vulnerability. Also… get_results method returns just an array of results. You can’t use it as an object and get num_rows from it – there is no such property in an array (it’s a property of … Read more

Syntax for $wpdb->prepare when searching in two columns

The part after WHERE decides which columns are searched. So you need to change it this way: $foods = $wpdb->get_results( $wpdb->prepare( “SELECT id, foodname, namevariations, calories, carbs, fat, protein, sodium FROM foodsTable WHERE namevariations like %1$s OR foodname like %1$s”, ‘%’ . $wpdb->esc_like( $search_text ) . ‘%’ ), ARRAY_A ); Also you were using LIKE … Read more

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