0

I would like to have different types of posts on my blog, e.g.: articles, film descriptions, book descriptions, links with short descriptions, quotations, historical events, characters, artwork, musical works, terms, etc. They should be grouped in hierarchical catalogues and hierarchical tags. Theoretically, this plug-in provides me with the possibility to do it: https://wordpress.org/plugins/custom-post-type-ui/. There is only a problem with it – custom post types don’t appear on the home page: https://wordpress.org/support/topic/custom-post-type-dont-appear-in-the-main-colllection/. I’m learning php, but it will take a lot of time before I get the right knowledge. I could hire a programmer, but first I would like to do a good research. I believe that there is another simpler way. Does anyone know it?

1

Find functions.php in your theme and add this code:

function add_custom_pt( $query ) {
  if ( !is_admin() && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'post', 'cptslug1', 'cptslug2' ) );
  }
}
add_action( 'pre_get_posts', 'add_custom_pt' );

Change array values to match your post type slugs (add as many as you need), but keep 'post'.

1

Use the pre_get_posts hook to modify the $query and include your custom post types in the search.

Add the following to your theme's functions.php file.

function modify_query( $query ) {
    if ( is_home() ) {
        $query->set( 'post_type', array( 'post', 'articles', 'film_descriptions', 'book_descriptions', 'characters' );
    }
}

add_action( 'pre_get_posts', 'modify_query', 10, 1 );
2
  • My bad, I'm in the habit of seperating actions and filters into their own files on a custom theme I've been working on. I've updated my answer, thanks for your comment! – Waldo Rabie Jan 21 '20 at 0:17
  • Since $query is defined globally and this is an action hook, nothing needs to be returned. I've incorporated your other suggestions into the answer. Thanks again! – Waldo Rabie Jan 21 '20 at 0:33

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.