Categories
CMS Development Wordpress

Weird orderby => post__in issue

I seem to have run into a sort of odd issue with WP_Query post__in I can’t quite figure out. ANY help is appreciated!

Goal/Progress so far:

Overall I want to merge two arrays and use the merged array inside a WP_Query. I currently have two get_posts queries, one has 5 posts (can be any number), the second has 2000+ posts. I’m then merging these and using the merged array inside WP_Query by saying 'post__in' => $mergedArray and I want to order the posts so the 5 come first, so i’m then saying 'orderby' => 'post__in' to maintain the same order as the merged array. The issue is this only works if the $mergedArray total count is below ~1340. Otherwise the result is just blank.

I’ve tested a lot of different ideas so far to try to narrow down the core issue. From what i can tell at this point it has something to do with the amount of posts in my second query. Also, you will see I am using a custom taxonomy but this doesn’t seem to affect the actual outcome overall. The issue persists with and without the tax_query.

My code:

         $queryOne = get_posts( 
            array( 
                'post_type' => 'post', 
                'fields' => 'ids',
                'posts_per_page' => -1,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'blog_post_loc',
                        'field' => 'slug',
                        'terms' => 'blog-hub-page'
                    )
                ),
            )
        );
        $queryTwo = get_posts(
            array(
                'post_type' => 'post',
                'fields' => 'ids',
                'posts_per_page' => 1330, // for some reason if we go higher than 1330 (or use -1) it breaks
                'tax_query' => array(
                    array(
                        'taxonomy' => 'blog_post_loc',
                        'field' => 'slug',
                        'terms' => 'blog-hub-page',
                        'operator' => 'NOT IN'
                    )
                ),
            )
        );
        $mergedArray = array_merge($queryOne, $queryTwo);
        $blogLoop = new WP_Query( 
            array( 
                'post_type' => 'post',
                'post__in' => $mergedArray,
                'orderby' => 'post__in',
                'posts_per_page' => 9,
                'paged' => $paged,
                'has_archive' => true
            )
        );

List of tests i’ve done so far:

If I remove 'orderby' => 'post__in' then the wp_query does output all 2000+ posts, however they are not the order i’m looking for. I also checked the count to see if this was indeed the merged array and it is and i can confirm by limiting or increasing the number of posts on the first query and checking the resulting count of the $mergedArray

If I take the args from the second query and put them directly into the WP_Query args then I get back all 2000+ posts as it should, so this tells me the actual args are fine.

Even if we forget for a moment about the merging of the two arrays, if I only use the second query and I don’t limit it at all, just tell it to grab all posts, then I go to use that array inside my WP_Query through 'post__in' and keep orderby as well, then it still has the same issue where its just blank. If i remove orderby then it shows all posts in the array.

If I limit the number of posts in the second query to around 1300 then everything works perfectly how I want (first query coming first, followed by second query) but i’m only getting about half of the total posts.

My assumption:

So what this all leads me to believe is either A) I’m just messing something up and im oblivious to what that is, or B) post__in and orderby has some limit on the number of posts it can handle, and/or maybe there is some param I can use to remove that limit.

Again any help at all is much appreciated. I’ve pretty much tested everything I can think to test and at this point if nobody else has any ideas then it may just have to stay at 1300 posts cause i’m out of options lol

Leave a Reply

Your email address will not be published. Required fields are marked *