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.

  • 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 Feb 22 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. – Sandbox Wizard Feb 22 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. – Sandbox Wizard Feb 22 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 Feb 23 at 0:29
  • Ah, okay. I hadn't thought about it that way. Thanks I'll look at that. – Sandbox Wizard Feb 23 at 0:49
0

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

  • 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. – Sandbox Wizard Feb 22 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. – MikeNGarrett Feb 22 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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