0

I have an action setup on pre_get_posts which I would like to use post__in on.

I used the following code as a test:

$query->set('post__not_in', array(74485,1528,71460));

This works, the total number of posts is reduced by those 3. However, what if I want to use post__in to just return those 3 posts? I tried:

$query->set('post__in', array(74485,1528,71460));

But that didn't work. I thought maybe it was because post__not_in is already being used (as the codex says post__in and post__not_in can't be used together. So I tried:

$query->set('post__not_in', '');
$query->set('post__in', array(74485,1528,71460));

But this still doesn't work.

Any help please? Thanks

  • I don't understand your actual question here. Are you trying to run two instances of pre_get_posts on the same query. If so, why? What are you trying to do – Pieter Goosen Jul 30 '14 at 12:09
  • I'm trying to filter the results of a query using pre_get_posts. Currently lots of results are returned, I'd like to only include those 3 post IDs (just for testing purposes, I have a much larger array of post IDS I'd like returned). At the moment I can exclude post IDs with post__not_in (this also works with the large array of IDS I have) but I can't include only them. I suppose another way would be to get an array of all post ids and compare that to the array I have? – patrickzdb Jul 30 '14 at 12:11
  • Are you using sticky posts – Pieter Goosen Jul 30 '14 at 12:14
  • I'm not sure, how would I check? This is a woocommerce product loop, so post_type=product – patrickzdb Jul 30 '14 at 12:15
  • I tried adding $query->set('ignore_sticky_posts', 1); but still no joy. – patrickzdb Jul 30 '14 at 12:27
2

I just ran into this with WooCommerce. I ended up passing the IDs I needed to a variable for safe keeping, and then hooking a function with extremely low priority. That worked fine. I figure woocommerce or what of my cleint's 8000000 plugins was overwriting post__in.

add_filter('pre_get_posts', 'assign_ids', 1000);
| improve this answer | |
  • Cheers, that's a nice method. I ended up using a different solution in the end. Could I see your code for assign_ids? – patrickzdb Aug 20 '14 at 8:50
  • MILION THANKS! the low priority solved my hugeeee problems! – T.Todua Jun 18 '15 at 22:26

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.