how to execute different sql query in non-sanitized $wpdb->get_results function

if $wpdb->get_results is not sanitized by core code, why we can’t execute different SQL query in it? Because the wpdb class uses (see wpdb::_do_query() ) mysqli_query() (by default) or mysql_query(), and unfortunately these functions do not support multiple queries such as SELECT <query>; INSERT <query>; SELECT <query>. Therefore, if you want to execute multiple SQL … Read more

Sql syntax error

The error in the SQL statement is at the very end, SELECT * FROM c_posts WHERE id = , There is no value being supplied to check against the id column. This is most likely because you didn’t check for a blank value in the ID field before forming and executing the query. Check the … Read more

Filter WordPress posts by between parameter

Tax Query Limits A Taxonomy Query in WordPress supports the following three arguments for the operator parameter: IN, NOT IN and AND. So it basically can’t do what you’re trying to do. Not even with advanced (tax_query) Queries. Meta Query and possibilities You’ll want to move your concept a bit and work with post meta … Read more

Database “Migration” for Plugins?

Surprisingly not. Instead, you need to use the following function which is run whenever the plugin is activated. define( ‘YOUR_PLUGIN_VERSION’, ‘1.0.0’ ); register_activation_hook( __FILE__, ‘your_plugin_activation_function’ ); function your_plugin_activation_function() { // Do activation tasks here. your_install_function(); your_upgrade_migration_function(); } Run your install script. function your_install_function() { // Set the current version of the plugin in the db. … Read more

Query to get the author who having maximum number of post (custom post type)

You can try with the following query, where the author data as well as the post count can be retrieved. $sql = “SELECT SQL_CALC_FOUND_ROWS wp_users.ID,post_count FROM wp_users RIGHT JOIN (SELECT post_author, COUNT(*) as post_count FROM wp_posts WHERE ( ( post_type=”custom-post-type” AND ( post_status=”publish” ) ) ) GROUP BY post_author) p ON (wp_users.ID = p.post_author) WHERE … Read more

How to execute mulitple statement SQL queries using $wpdb->query?

TL;DR This is not possible with wpdb. Use dbDelta(). Even better, use something else to run raw queries. Explanation wpdb->query() uses the mysql_query() function. From PHP manual: mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that’s associated with the specified link_identifier. In order for that … Read more

Get posts by category with pure SQL query

If you want to be able to get the category by name, this should work: SELECT DISTINCT substr(post_title, – instr(reverse(post_title), “https://wordpress.stackexchange.com/”) + 1) AS year FROM {$wpdb->posts} p LEFT JOIN {$wpdb->term_relationships} rel ON rel.object_id = p.ID LEFT JOIN {$wpdb->term_taxonomy} tax ON tax.term_taxonomy_id = rel.term_taxonomy_id LEFT JOIN {$wpdb->terms} t ON t.term_id = tax.term_id WHERE post_status=”publish” AND … Read more

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