Query does not filter duplicate _sku numbers

I haven’t tested this, but this should give you the count of skus for each post_id: SELECT post_id, COUNT(`post_id`) AS sku_count FROM `wp_postmeta` WHERE `meta_key` LIKE ‘%sku%’ GROUP BY `post_id` To have it return only rows where the count is greater than one is trickier. Off the top of my head I’d probably use the … Read more

Custom MySQL query to pull out Advanced Custom Fields?

The code is fine, I can’t see any need to modify the query – I think you’re missing the third argument in your get_post_meta call: $postACF_sc = get_post_meta($postId, ‘sc_link’, true /* $single */); You might also want to add this snippet just before the foreach to save multiple database queries: update_postmeta_cache( wp_list_pluck( $posts, ‘posts_id’ ) … Read more

How to make MySQL search queries with quotes

Woah there. You’ve just opened up a can of SQL injection. I use the default get_query_var(‘s’) that I believe is automatically escaped by wordpress. Not quite – get_search_query() will do that, but get_query_var( ‘s’ ) gets the “raw” value. Regardless, always use wpdb::prepare or similar escaping before executing SQL: $query = $wpdb->prepare( “SELECT * FROM … Read more

Use custom query if main search query returns zero results in wordpress

Exactly right about resetting it! You should use wp_reset_query(). Try this: $num_res = $wp_query->post_count; $get_search_term = get_search_query(); // No more work required for the query, so… wp_reset_query(); if($num_res == 0) { $args = array( ‘post_type’ => ‘resources’, ‘meta_key’ => ‘resource_txt’, ‘meta_value’ => $get_search_term, ‘meta_compare’ => ‘LIKE’ ); $custom_query = new WP_Query( $args ); // Do … Read more

Mysql Queries per Visit – Crazy High

It goes against the grain here to recommend plugins but that’s what you need here. To view in the browser; https://wordpress.org/plugins/query-monitor/ To view in WP-CLI; https://runcommand.io/wp/query-debug/ Both of these will list the queries and how long each one takes. That should give you enough information to make informed decisions about what to do in your … Read more