pre_get_posts gives 404 error on Custom Post


I know this has been covered in different ways a thousand times (I read all the posts), but it seems not specifically for my case. I can’t really get it to work myself, being a big development noobie.

I have a template which shows the last posts on the front page. I changed the type of posts shown to question (my website is a Q&A one) but then pagination was only showing 13 pages while there are 29 pages of questions. 13 pages being the number of non-custom-type normal posts.
So I added this code in functions.php:

add_action( 'pre_get_posts', function($q) { 
    if( !is_admin() && $q->is_main_query() && !$q->is_tax() ) { 
        $q->set ('post_type', array( 'question' ) ); 

Now it counts the number of Questions correctly but it throws a 404 error every time I click on the link to a question. I understand that pre_get_posts is not the best approach, but I really don’t know how to change it and use WP_Query instead, as I saw was advised to.

Any fix to that ? Thank you <3

, , , Lisa Lucas 6 years 2017-07-03T14:43:55-05:00 0 Answers 238 views 0

Leave an answer