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…

6 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…

6 years ago

Disable update notification for individual plugins / All

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

6 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-67a5ae63b5662587354645/] or with readmore [crayon-67a5ae63b5665391574962/]  

6 years ago

Hide page visual editor if certain template is selected

[crayon-67a5ae63b56ff109712785/]  

7 years ago