Hot answers tagged

2

You have a problem with your query which is you can't use GROUP BY and ORDER BY before WHERE replace FROM tablename GROUP BY submit_time ORDER BY submit_time DESC with FROM tablename This is your query $results = $wpdb->get_results( 'SELECT DATE_FORMAT(FROM_UNIXTIME(submit_time), "%b %e, %Y %l:%i %p") AS Submitted, ...


2

Going to take a wild stab, but ARRAY_A will return an associative array You are then trying to access this associative array as an object Simple change: foreach($results as $result){ echo $result->Name; } To: foreach($results as $result){ echo $result['Name']; } and the error should go away!


2

You should use $wpdb->prepare for queries, as it does all the sanitizing and escaping for you. Also, key is reserved as a field name in MySQL: function ch_details_from_id($id) { global $wpdb; // or try using " . $wpdb->prefix . "ch_guests $query = "SELECT * FROM $wpdb->ch_guests WHERE guest = %d"; $details = $wpdb->get_results( ...


2

Just use %s for string and %d for digit replacements. Do not forget to use the proper esc_*() functions (use full text search for esc_ in the function reference). global $wpdb; $wpdb->show_errors = true; $wpdb->suppress_errors = false; ! defined( 'DIEONDBERROR' ) and define( 'DIEONDBERROR', true ); $sql = <<<SQL SELECT ID FROM ...


2

Is this what you're trying to do? // WP_Query arguments $args = array ( 'post_type' => array( 'project' ), 'meta_query' => array( array( 'key' => 'meta_key', 'value' => '%$query%', ), ), ); // The Query $query = new WP_Query( $args );


2

No, wpdb::delete does not handle anything other than WHERE field = X. You can just use the query method instead: $ids = implode( ',', array_map( 'absint', $ids ) ); $wpdb->query( "DELETE FROM table_name WHERE ID IN($ids)" );


1

post__in accept array of post IDs e.g. array(5, 4, 8, 9); Extending @bravokeyl comment. By default result is Object. As codex suggest you can change the output type of get_results() to these values. OBJECT - result will be output as a numerically indexed array of row objects. OBJECT_K - result will be output as an associative array of row objects, ...


1

An alternative, instead of writing SQL queries by hand, is to use e.g.: $post_ids = get_posts( [ 'fields' => 'ids', 'post_type' => 'attachments', 'orderby' => 'menu_order', 'order' => 'ASC', 'post__in' => wp_parse_id_list( $input_ids ) ] ); where the handy wp_parse_id_list() core ...


1

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 $details = $wpdb->get_results("SELECT * FROM " . ...


1

I'm not an expert but try this one: SELECT wp_posts.ID, wp_postmeta.meta_value FROM wp_postmeta LEFT JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id WHERE wp_postmeta.meta_key = '%$query%' AND wp_posts.post_type = 'project' it worked for me


1

Try adding the single quotes back around the %s: $countthem = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM wp_cmapg WHERE lang='%s'", $lang) );



Only top voted, non community-wiki answers of a minimum length are eligible