I'm trying to retreive a set of custom posts using the following:
// set search conditions
$args = array(
'posts_per_page' => -1,
'post_type' => 'fee',
'meta_key' => 'fee_code',
'meta_value' => $postmeta['fee_code']
);
// get results
$the_query = new WP_Query( $args );
The 'fee_code' is coming from elsewhere in my plugin and contains a 5 digit number (int). If I view the SQL that is being generated it gives me the results I want (in this case 2 results).
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1 = 1
AND (( wp_postmeta.meta_key = 'fee_code'
AND wp_postmeta.meta_value = '13632' ))
AND wp_posts.post_type = 'fee'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10;
If I run this SQL in phpMyAdmin I get the 2 results I want, but when I run the plugin in WordPress I get 10 posts returned, none of which have '13632' (the fee code I'm looking for in this example) in any field associated with the post.
On other searches I get the results I'm looking for. I can't figure out why in this case I get so many extra, unrelated results.
Any ideas what I might be doing wrong?
ADDITION: I have another query I was trying to use, which looks at two criteria.
$args = array(
'posts_per_page' => 25,
'post_type' => 'fee',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array( // find matches in fee code field
'key' => 'fee_code',
'value' => $postmeta['fee_code']
),
array( // find matches in grouped fees section
'key' => 'grouped_fees',
'value' => $postmeta['fee_code'],
'compare' => 'LIKE'
),
)
);
The first part of the meta_query should be the same as my original question and the second is looking for the same fee code in a custom field where it is grouped with a number of other related fees. This query also returns the same unrelated posts for this search and the appropriate results for other searches.
WP_Query
doesn't generate a query, it generates multiple queries. Also, you never mentioned if you have filters onpre_get_posts
, or where the$postmeta
array comes from. I would also keep in mind that looking for posts via their post meta can be extremely expensive/slow/taxing on a server, that's what taxonomies were added for, is there a particular reason you didn't use a custom tax namedfee_codes
? It would've been potentially 100-1000x faster, easier to write queries for, and given you free templates/urls$the_query->request
to see the query that was used and am looking at$the_query->posts
to see what has been returned. I don't have any filters onpre_get_posts
. I'll look into using custom taxonomies, but this process is for updating fee values and is used once a year, so I'm not too worried about performance for the time being.fees
, each fee has its ownfee_code
would it make sense to use a custom taxonomy for thefee_code
? My understanding of taxonomies was that they are used to group objects together. There is no overlap between fee_codes, it would be a 1 to 1 relationship.