wp query - Strange results from WP_Query

admin2025-06-02  3

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.

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.

Share edited Feb 22, 2019 at 22:35 Sandbox Wizard asked Feb 22, 2019 at 19:50 Sandbox WizardSandbox Wizard 771 silver badge7 bronze badges 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. – Sandbox Wizard 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. – Sandbox Wizard 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. – Sandbox Wizard Commented Feb 23, 2019 at 0:49
 | 

1 Answer 1

Reset to default 0

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748875354a314446.html

最新回复(0)