Query WordPress database by registered date and role

I’m no SQL expert, but I think it would look something like this: SELECT * FROM mydatabase.wp_users INNER JOIN mydatabase.wp_usermeta ON (wp_users.ID = wp_usermeta.user_id) WHERE 1=1 AND wp_users.user_registered > ‘2014-03-15 00:00:00’ AND wp_users.user_registered < ‘2014-03-16 00:00:00’ AND ( (wp_usermeta.meta_key = ‘wp_capabilities’ AND CAST(wp_usermeta.meta_value AS CHAR) LIKE ‘%\”subscriber\”%’) ) ORDER BY user_registered ASC ;

Custom query to get terms from post ids

It is not advised to make use of custom SQL queries if there are functions in WordPress that already does the job. To accomplish this, you can simply make use of wp_get_object_terms(). The first parameter $object_ids takes a string or an array of object ID’s. In your case, you can make use of an array … Read more

SELECT * FROM $wpdb->posts WHERE ID > 160

SELECT * FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id) LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) WHERE $wpdb->posts.post_type=”post” AND $wpdb->term_taxonomy.taxonomy = ‘post_tag’ AND $wpdb->posts.ID > 160 ORDER BY $wpdb->posts.ID ASC LIMIT 5 $pageposts = $wpdb->get_results($querystr); if ($pageposts): ?> <?php global $post; ?> <?php … Read more

I want to change the media list with additionally query

As said in comments, it’s not a good idea editing WP core files. You can easily modify the query that displays your media posts by adding the following code to a plugin or your theme’s functions.php add_filter( ‘pre_get_posts’, ‘_wp_media_pre_get_posts’ ); function _wp_media_pre_get_posts( $wp_query ) { global $pagenow; if( ! in_array( $pagenow, array( ‘upload.php’, ‘admin-ajax.php’ ) … Read more

Advanced WP Query hogs the SQL server

There are several options: Use some Lucene-based search index and cache your queries there. Power it with lots of RAM so nothing hits your DB after the first query. Use something like… Elasticsearch Solr Sphinx Do not attempt to run Lucene plain … you will find yourself in hell. Nobody does that for a good reason. … Read more

Get WooCommerce product attribute taxonomies in a SQL query on WordPress database

With WordPress to make a custom SQL query, you should always use the dedicated WPDB Class and related methods as get_results(), this way: global $wpdb; $results = $wpdb->get_results( ” SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies ” ); if ( count($results) > 0) { $data = []; // Initializing // Loop through results objects foreach( $results as $result … Read more

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