CONCAT_WS in custom sql query

‘CONCAT_WS(‘,’, wp_postmeta.meta_key, wp_postmeta.meta_value)’ was the name of the column. Everything is working as it should. To change the name of the column, add AS name after the CONCAT_WS statement, like this: ‘CONCAT_WS(‘,’, wp_postmeta.meta_key, wp_postmeta.meta_value)’ AS name (I didn’t test this specifically). Now the new column will be called name.

Select latest 2 articles, but only from given category

Try the below code- SELECT p.*, t.term_id FROM wp_posts p LEFT JOIN wp_term_relationships rel ON rel.object_id = p.ID LEFT JOIN wp_term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id LEFT JOIN wp_terms t ON t.term_id = tax.term_id WHERE tax.taxonomy = ‘category’ AND post_type = “post” AND post_status = “publish” AND t.term_id != 56 ORDER BY ID DESC LIMIT … Read more

$wpdb post type and term query only works when there are no dashes or spaces in the term slug and title

In your code you are using “AND terms.name=”shorelineorwaterway” where I don’t think shorelineorwaterway is your term name, it could be your terms slug. So you can modify this by “AND terms.slug = ‘shorelineorwaterway’ and here inside inverted commas you can use hyphen. If you need to use term name then “AND terms.name=”Your Term Name” would … Read more

mysql query on wp_user and user_meta problem

In usermeta table WordPress stores many serialized data. So direct meta_value would not work I think. So try the below query- SELECT a.id, a.user_email, a.user_registered, a.user_login, b1.meta_value AS first_name, b2.meta_value AS last_name, b3.meta_value AS qualified, b4.meta_value AS referrer, b5.meta_value AS view_type, b6.meta_value AS ref_by, b7.meta_value AS wp_optimizemember_custom_fields FROM wp_users AS a INNER JOIN wp_usermeta AS … Read more

$wpdb->get_var not returning count

In your code it is actually counting the query you have inserted in $testargs. In your case you entered one. So it is giving you back one. If you need to get the count on WordPress users table then write the query like below- global $wpdb; $total_query = “SELECT COUNT(*) FROM {$wpdb->users} AS total_count”; $total … Read more

Add row to SQL array

What about something like this ? $custom_row = array(array(‘race_id’ => ‘1’, ‘race_name’ => ‘2’, ‘race_date’ => ‘3’)); $races = $wpdb->get_results(” select r.race_name ,r.race_id ,date_format(r.race_date,’%c.%d.%Y’) race_date from race_calendar r order by r.race_date; “, ARRAY_N); $merged_arr = array_merge($custom_row, $races); foreach ( $merged_arr as $race ) { echo $race[‘race_id’] . ‘,’ . $race[‘race_name’] . ‘,’ . $race[‘race_date’]; } … Read more