1

Found below snippet to pull posts from a custom post type 'pictures' into the home page and date archive page..

Added to functions.php this works for home page:

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'pictures') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

But this works for neither:

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_date() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'pictures') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

Would also like to add in !is_admin() to keep the admin screens clear, and not sure how to go about that.

thanks,

2

In your code ( $query->is_home() && $query->is_date() && $query->is_main_query() ) always returns false because $query->is_home() and $query->is_date() cannot be true on same page.
Try this,

function add_custom_post_type_to_query( $query ) {
    if ( ! is_admin()  &&  $query->is_main_query() ) {
       if ( $query->is_date() ||  $query->is_home()  ) {

           $query->set( 'post_type', array('post', 'pictures') );

       }
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

I hope this helps!

  • 1
    Just superb! thanks Qaisar – MatJon Mar 23 at 8:35
  • @MatJon Stay blessed! – Qaisar Feroz Mar 23 at 10:12
  • @MatJon if this worked for you, then mark this answer as "Accepted Answer" to help other people searching for similar solution. – Qaisar Feroz Mar 23 at 12:08

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.