Articles of wp query

Can I count matches without doing a query?

I’m trying to count results for a given query, but I don’t actually need the posts so I’m trying to figure out if there’s a way to count posts without querying them. Something similar to I’m counting posts by meta key/value so wp_count_posts() won’t work. Here’s what I’m currently doing: $query = new WP_Query(array( […]

Query total number of posts

Using a search filter plugin. I am using to get the total number of posts that exist in the database $wp_query->found_posts However, when a user filters the results on the page, this number changes according to how many posts are shown on the filter. How could I get the static total number of posts which […]

How can I convert results of WP_Query (of Custom Post Type, with Custom Fields) to JavaScript?

I am using DataTables on this page. It works great, except I’ve had trouble getting child rows to work. I believe what I need to do is to convert the WP_Query results with associated custom fields into a JavaScript array. It is easy enough to get the results of WP_Query in this manner, but when […]

Query with custom taxonomy not working

I have custom post type ‘mix‘. And custom taxonomy ‘meal‘. And categories in custom taxonomy (one of that is with slug: breakfast) I have problems with making query. My code is : $args = array( ‘post_type’ => ‘mix’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘meal’, ‘field’ => ‘breakfast’ ) ), ‘posts_per_page’ => 50 ); $query […]

How to filter by category in REST API, excluding posts also in other category term?

I’m using REST API to load a several posts from other site, each post must belong to a different category. However I have one “Featured events” category, that includes posts also shown in other category, so I have a post repeated in two different categories. My query for the featured category is like this: http://localhost/wp-json/wp/v2/events/?categories=23&per_page=1 […]

Return Taxonomy/Term Information with Posts (WP_Query/get_posts)

Is it possible to return taxonomy/term information for posts along with the posts themselves when querying with get_posts? I have around 150 items I am querying so I don’t want to loop and pull each individually.

Complex date range with WP_Query and BETWEEN

Backstory We have a bunch of events inside an events custom post type. They each have the following custom fields associated to them: start_date end_date On the front-end, we have a sidebar with filters where the user can choose how they want to search events (ie., various taxonomies, start date for their trip in town, […]

Loop through all product posts?

I want to loop through all my posts who are called ‘products’. For that I use this one: $products_IDs = new WP_Query( array( ‘post_type’ => ‘product’ )); while ($products_IDs->have_posts() ) : $products_IDs->the_post(); $y = $y + 1; $myfile = fopen(“wp_all_import.txt”, “a”); fwrite($myfile, “Counter: ” . $y . “\t”); fclose($myfile); //some code… $sql = “INSERT INTO […]

Can't get order_by meta_value_num to work properly

This query is working ALMOST as intended, but for some reason I can’t get it to order by the custom field opl_submission_tempo. That field is set up to be a number, so I’m not sure where I went wrong here. $tempo_query = new WP_Query( array( ‘post_type’ => ‘opl_tempo_submission’, ‘order_by’ => ‘meta_value_num’, ‘meta_key’ => ‘opl_submission_tempo’, ‘order’ […]

Woocommerce query by price range and custom meta key

I’ve written a function to get products by price range. All works, but now I need add a extra meta key, that will be like 50 – 100 and featured, but code is not returning any products. What is wrong on this code? function product_price_filter_box($attr) { $limit = intval($attr[‘limit’]); $min = intval($attr[‘min’]); $max = intval($attr[‘max’]); […]