0

i do not know much about coding and I found code online to help me add a custom post type to my wordpress home page but when i tried to use the same code again to add another custom post type the posts won't display. If i only add the first post type, that code will display the first function. As soon as I add another post type after the first one, none of my custom post types will display. I am assuming I need to add something between them? I have tried } but that does not work. Please, any help will be greatly appreciated. :)

FIRST FUNCTION

function first_function( $query ) {
 if ( is_home() && $query->is_main_query() )
 $query->set( 'post_type', array( 'post', 'first_function') );
 return $query;
 }
 add_filter( 'pre_get_posts', 'first_function' );

SECOND FUNCTION

function second_function( $query ) {
 if ( is_home() && $query->is_main_query() )
 $query->set( 'post_type', array( 'post', 'second_function') );
 return $query;
 }
 add_filter( 'pre_get_posts', 'second_function' );
1

Your second function overwrites the query of the first. It's not necessary anyway as you can add as many post types to the $query->set line in the first function as you need, like this:

$query->set( 'post_type', array( 'post', 'first_function', 'my-second-post-type','another-post-type' ) );

BTW, is your post type actually called 'first_function'? Just asking as that's the name of the function too.

Hope that helps

| improve this answer | |
  • Thank you so much! I will try this method and see what I get. No it's not called first_function. I think I was trying to be as descriptive as I could but being a newbie, it didn't go over too well. lol. Thanks again! – Ms Vibrant Feb 16 '18 at 16:26
  • No worries. If it works for you, don't forget to come back and mark the answer as accepted :-) BTW - always best to be as accurate as possible if posting code - don't change it as you might just change the bit that's causing the problem – Peter HvD Feb 16 '18 at 16:31
  • This worked perfectly! Thank you for the advice and thanks again for your quick solution. Have a wonderful weekend! – Ms Vibrant Feb 16 '18 at 16:48
  • You're welcome. One last bit of advice - change the line if ( is_home() && $query->is_main_query() ) to if ( !is_admin() && is_home() && $query->is_main_query() ) - you're already testing for the home page, so it shouldn't be necessary, but by including !is_admin() you're making sure that it won't ever be run on the admin side of your site (ie, the "Dashboard")... belt & braces really! – Peter HvD Feb 16 '18 at 17:19
  • Awesome! I changed to what you suggested and it seems to be working fine. Thanks again for your help! :) – Ms Vibrant Feb 16 '18 at 18:13

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.