0

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.

5
  • What does the code that tests this query object look like? Keep in mind that WP_Query doesn't generate a query, it generates multiple queries. Also, you never mentioned if you have filters on pre_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 named fee_codes? It would've been potentially 100-1000x faster, easier to write queries for, and given you free templates/urls
    – Tom J Nowell
    Commented Feb 22, 2019 at 21:38
  • I'm using $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 on pre_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. Commented Feb 22, 2019 at 22:30
  • @TomJNowell I have a custom post type for fees, each fee has its own fee_code would it make sense to use a custom taxonomy for the fee_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. Commented Feb 22, 2019 at 23:42
  • They're usually used to group things together, but that's just one way of viewing it. Think of it instead as filtering out other things. E.g. a category lets you find all posts that have X, which is what you're doing in your post meta query. So why not do the same with fee codes? There's no harm in having just 1 post in each term. Post meta tables are optimised for finding values when you already know the attached post ID. Taxonomies are optimised for finding post IDs when you already know the value
    – Tom J Nowell
    Commented Feb 23, 2019 at 0:29
  • Ah, okay. I hadn't thought about it that way. Thanks I'll look at that. Commented Feb 23, 2019 at 0:49

1 Answer 1

0

You want to use posts_per_page instead of numberposts. Take a look at the Pagination Parameters for WP_Query.

2
  • Thanks @Mike, that was wrong, but isn't affecting my results. I've updated that in my code, but I still get the 10 unrelated posts. If I change posts_per_page to 15 I still get the same 10, so it wasn't that the LIMIT 0, 10 was holding the results at 10. Commented Feb 22, 2019 at 20:16
  • There's nothing else that stands out to me. A couple things to try: use a meta query, set post_status to publish, set post_type to any, or set posts_per_page` to a large number. Commented Feb 22, 2019 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.