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.