×

How to add custom post type in jetpack publicize

custom post and jetpack publicize

While developing a new WordPress plugin for CBSETODAY.COM, I suddenly realized that the website is able to publicize all the default post types of on all the listed social networking websites in Jetpack socialize tool automatically but custom post were excluded.

Jetpack is one of the most used WordPress plugin, right now able to do a lots of task for you and maintained by the developer of WordPress –“Automatic”.

It was ridiculous to notice that jetpack is able to manage all the custom post type supported by automatic itself like Woocommerce but was not allowing me to add my own custom post in his syndication. Why ?

So start digging deeper WordPress then realize that WordPress loop also does not automatically include your defined custom post type on its loop.

Steps to add custom post type in WordPress loop

To make changes in your WordPress loop, add the following code inside your theme’s functions.php file

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

The above code will force my custom post type in its main loop. ‘Pre_get_posts’ filter add this filter before sending data to the main loop.  In this code  we are only making the changes in only home page.

But the changes will not be available to the syndicated feeds. So if you want to add this custom post type ‘course’ in the feed of this website.  You are also required to  add the following code in place of

Previous if statement. Now your whole query will look this

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

But the above code will not help you to send this published custom post on social networking site like facebook pages or twitter account.
Jetpack’s publicize feature is very easy to implement but it can not be implemented through functions.php file.  You have to find out the file – where this custom post had been defined.

Before Editing your functions.php file it is always advisable to take backup of your theme and do the needful off line then update the same using FTP

And make this changes in that file

'supports'  => array(  'title',  'editor',  'excerpt',   'publicize',   'tag',  'thumbnail'    ),

Now your whole code will look something like this.

custom post and jetpack publicize

Now save your work and check the results.

Related Posts

Leave a Reply

Your email address will not be published.