Get a count of how many times a term or a category is used in posts

Wp_Query should be able to do this for you. Pass it the appropriate parameters, including a tax_query, and check found_posts. $p = new WP_Query( array( ‘post_type’ => ‘custcpt’, // your CPT ‘tax_query’ => array( array( ‘taxonomy’ => ‘custtax’, // your tax ‘field’ => ‘id’, ‘terms’ => $cat->term_id, // your term ID ) ), ‘ignore_stickie_posts’ => … Read more

Why this query is not showing any result on wordpresss home page?

First of all your query is wrong. The right query is $mypost = $wpdb->query($wpdb->prepare(“SELECT post_content FROM $wpdb->posts WHERE ID = %d”, 2)); Second the $wpdb->query will returns an integer value indicating the number of rows affected/selected. You can check here http://codex.wordpress.org/Class_Reference/wpdb#General_Syntax If you want generic result you can use $wpdb->get_results http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results $mypost = $wpdb->get_results($wpdb->prepare(“SELECT post_content … Read more

List of ways to access WordPress database?

There’s some overlap, but each isn’t an abstraction of the previous, and there are definitely some things you don’t want to do outside of the API. Most things, actually! WP_Query is for retrieving posts and pages, it doesn’t update anything, and it doesn’t fetch data that isn’t a post or page. The higher level API … Read more

Custom Query – Based on user input

While this is primarily a SQL question, there are WordPress components and Core has no efficient mechanism to pull this data that I am aware of. This has long been a problem in Core in my opinion. So: global $wpdb; // group name key $meta_key = ‘_create_new_group’; if (!empty($_REQUEST[‘q’])) { // user input $search = … Read more

Posts modified in the last 48 hours

You can do it easily with WP_Query: $args = array( ‘date_query’ => array( ‘relation’ => ‘OR’, array( ‘column’ => ‘post_date’, ‘after’ => ‘-2 days’ ), array( ‘column’ => ‘post_modified’, ‘after’ => ‘-2 days’ ) ) ); $query = new WP_Query( $args ); More information about date_query parameter of WP_Query. If, for some reason, you still … Read more

Getting additional columns from sql

You need to add another join: INNER JOIN $wpdb->postmeta rate_review ON p.ID = rate_review.post_id Add the condition to your where clause: AND rate_review.meta_key = ‘rate_review’ Now you can add it to your SELECT: rate_review.meta_value AS rate_review

$wpdb update query in plugin only updating one column

I had to use two separate queries. The $wpdb->update syntax cant deal with calculating based on the existing value. But for the other two fields it works. So like this: $table = $wpdb->prefix . ‘woocommerce_downloadable_product_permissions’; $expiry_date = date(“Y-m-d”, strtotime(“+ 30 days”)); $expiry_time = $expiry_date . ‘ 00:00:00’; $wpdb->query(“UPDATE $table SET download_count = (download_count + 1) … Read more