Categories: Snippets

Adding or Including custom post type to default WordPress loop

Adding or Including custom post type to default WordPress loop by altering the ‘pre_get_posts’ function. Here it use add_action() function. This will help to display those posts (custom posts) alongside the default posts in the regular post feed.

Add this in you functions.php file

function add_custom_post_type_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    // if ( is_home() ) { // Use this if its not for main query
        $query->set( 'post_type', array('post', '2nd-post-type', '3rd-post-type') );
    }
}
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

 

Recent Posts

How to Limit or Disable WordPress Post Revisions

Limit the number of posts revisions that WordPress stores in the database. edit wp-config.php file…

5 years ago

Multisite – List Recent Posts Across an Entire Network

In Multisite, List Recent Posts Across an Entire Network by using custom query or WP_Query…

5 years ago

Disable update notification for individual plugins / All

code for the plugin file itself:- [crayon-663615f3412c6226648453/] I like to put this in the actual…

5 years ago

How to change the WordPress permalink structure to remove dates from the URLs

Switching the permalink structure from /year/month/post-slug to just /post-slug is easy by going to Settings…

6 years ago

Limit character in content and title in wordpress

[crayon-663615f3416cc027743247/] or with readmore [crayon-663615f3416d4043960662/]  

6 years ago

Hide page visual editor if certain template is selected

[crayon-663615f341864264613349/]  

6 years ago