0

Here is the code I gathered off the internet so far:

$args = array(
    'post_status' => 'publish',
    'posts_per_page' => '5000',
    'post_type'  => 'post',
    'meta_query' => array(
        'relation' => 'AND',
        'is_part_of_series' => array(
            'key'     => 'epic-series-select-series',
            'value' => $series_id,
            'compare' => '=',
        ), 
        'chapter_order' => array(
            'key'     => 'epic-series-chapter-number',
            'compare' => 'EXISTS',
        )
    ),
    'orderby' => array(
        'chapter_order' => 'ASC',
    ),
);

but for some reason, this gets the correct 3 posts plus some other posts - which I have no idea why are present. I checked the meta table in MySQL and only the correct 3 posts have the meta key with the value I am querying. Apparently, I am doing something wrong in the query! Any ideas?

I tried asking ChatGPT for help but it did not.

EDIT 1:

Here is a screenshot of the only posts that have epic-series-select-series meta key: enter image description here

12
  • noting that what you're doing does not scale well with post meta as the post meta table is not optimised for searches/filtering, if you changed the is_part_of_series post meta into a series taxonomy it would provide enormous performance improvements and speed ups. Also 5000 is a very very high number of posts, for reference the REST API limits to 100 max and Yoast SEO uses 1k as an upper limit for a limited query and a lot of sites struggle with that. Can you share the values you're searching for and their format?
    – Tom J Nowell
    May 17 at 11:49
  • The $series_id I am searching for is 32881 May 17 at 14:40
  • I've also added a screenshot with all posts having that meta key. May 17 at 14:45
  • 1
    while you could use post IDs as slugs in a hidden/private taxonomy, or you could assign the taxonomy to both post types and look up which series are also in that term, there are many ways to do it, but it looks like your posts only have a single series ID and that post parent may be a much easier faster solution
    – Tom J Nowell
    May 18 at 14:49
  • 1
    wether a post type is hierarchical is a runtime memory thing, the database itself has no concept of non-hierarchy, so you can set the parent of a post that is non-hierarchical
    – Tom J Nowell
    May 22 at 11:53

0

Your Answer

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

Browse other questions tagged or ask your own question.