Post injection – how to exclude the original post

This question refers to @Pieter Goosen´s extensive work about how to alter the main query using the pre_get_posts hook on true pages. It is also referenced in the codex.

With his method you are able to inject specified posts in your page without having to touch the template file itself. This gives numerous development options.

Until now his method uses CSS to hide the post object which the standard loop outputs for the page that is displayed. There are two actions hooks, loop_start and loop_end that are used to create a hidden container around the post object. It would be great if it was possible to completly exclude the page´s post object.

I tried to hook into pre_get_posts with something like $query->set('post__not_in', get_the_ID(); at loop_start but this didnt work.
I am lacking the skills needed to put the finishing touches on this.

Can you help? Remember we dont want to touch the template files. Thanks in advance.

2 s
2

I think pre_get_posts should work with $query->set().
Only problem I see there is you are using get_the_ID() in a hook. Try using global $post variable to get the current post id.

Leave a Comment