WP_Query to display number of custom post type filtered (order by) taxonomy

You could try running a custom query for each: $args_1 = array( ‘post_type’ => ‘incidents’, ‘post_status’ => ‘published’, ‘posts_per_page’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘store’, ‘field’ => ‘name’, ‘terms’ => ‘store_1’, ), ), ); $count_1 = count( get_posts( $args_1 ) ); echo ‘Store 1: ‘ . $count_1 . ‘ posts’;

One of two similar WP Query is very slow

It is likely there are very few rows WHERE AND ( wp_posts.post_date >= ‘2019-09-04 21:45:00’ ) there are likely many thousands of rows WHERE AND ( wp_posts.post_date < ‘2019-09-04 21:45:00’ ) and a table scan is required. EXPLAIN SELECT SQL_NO_CACHE remainder of query for each and examine results carefully. Post the TEXT RESULTS of EACH … Read more

How to get only present and past posts with post_date

You need to pass a strtotime() compatible string or an array instead of ‘today’. You have several options… You could change ‘today’ to ‘now’. This will use the current time not midnight last night. You could change ‘today’ to ‘today + 1 day’. This will be the end of today. Not exactly what you are … Read more

Select multiple categories with is_tax

Thanks to @SallyCJ for the correct answer — you can supply an array of category IDs/slugs/names to is_tax() to check for multiple categories. Here is the correct code: /* Hyde out of stock product specific category */ add_filter( ‘pre_get_posts’, ‘hide_out_of_stock_from_cat’ ); function hide_out_of_stock_from_cat( $query ) { if ( $query->is_tax( ‘product_cat’, array( 15, 16, 18, 19, … Read more

How to get the latest post list (of the parent custom custom type)

First off, adjust your query so you’re only returning top level posts (the parent posts in this instance) add_filter(‘pre_get_posts’, ‘only_top_level_posts’); function only_top_level_posts($query) { // wrap some conditional statements around here so it doesn’t affect every query, just limit to whatever queries you need it in $query->set( ‘post_parent’, 0 ); return $query; } This will return … Read more

How to get meta key list efficiently?

As mentioned in the comments you can query the postmeta table directly: public function get_metadata_keys(){ global $wpdb; $meta_query = $wpdb->get_results( ” SELECT DISTINCT meta_key FROM {$wpdb->postmeta} ” , ARRAY_A ); $meta_keys = wp_list_pluck( $meta_query, ‘meta_key’ ); return $meta_keys; } This will return the list of all meta_keys associated to any post.