Support » Developing with WordPress » CPT On Blog Page, Feed And CPT RSS Feed Links

  • I have searched and searched and just cannot seem to find an answer for this.

    The goal is to display CPT on the blog and feed along with each CPT having its own RSS feed link.

    The first code below will list the CPT on the blog, but not on the blogs RSS feed. However, it allows the CPT to have its own RSS feed link:

    ie:
    https://domain/feed?post_type=example
    https://domain/feed?post_type=example-two

    // Adds Custom Post Type To Home And Feed
    function home_feed_loop( $query ) {
        if ( is_home() && $query->is_main_query() )
            $query->set( 'post_type', array( 'post', 'example', 'example-two') );
     return $query;
     }
     add_filter( 'pre_get_posts', 'home_feed_loop' );

    The second code below allows the CPT on the front page and the blogs RSS feed. However, it removes the ability for the CPT to have its own RSS feed link:

    ie:
    https://domain/feed?post_type=example
    https://domain/feed?post_type=example-two

     // Adds Custom Post Type To Home And Feed
    function home_feed_loop( $query ) {
        if ( ( is_home() && $query->is_main_query() ) || is_feed() )
            $query->set( 'post_type', array( 'post', 'example', 'example-two') );
     return $query;
     }
     add_filter( 'pre_get_posts', 'home_feed_loop' );

    Where am I going wrong?

    I would like to obtain all functionality. As in, have the CPT on the blog, in the blog feed and each CPT also have its own rss feed link.

    • This topic was modified 4 months, 1 week ago by  bward.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    You have a catch-22 situation in feed logic :/

    If you do not use is_feed(), you cannot get all post types in a feed.
    If you DO use is_feed(), you cannot override post types in the URL.

    Without is_feed(), you could get multiple post types with
    example.com/feed/?post_type[]=post&post_type[]=example&post_type[]=example-two
    Not very elegant.

    In order to use is_feed() so that multiple types can be included, yet still be able to override with a specific URL query var, your callback code would need to extract any query string from $_SERVER['REQUEST_URI']. If the string contains a “post_type” parameter, the request is being overridden, so do not apply the $query->set() code. Thus the override is honored, otherwise set the $query query var to send all post types as default feed behavior.

    Thanks for the responses.

    Joy, I prefer to use code to keep the site as light as possible. Might be a good option though.

    Mr. BC. That sounds a bit over my head at this point. I think I will have to revisit this one when I have some more time. Thanks for giving me something to go on though!

    I prefer to use code to keep the site as light as possible.

    Not sure what this means. It’s code either way. The plugin author already figured it out though. If you don’t want to use a coded and tested solution, you can at least read the code to see how.

    Ahh bad explaining on my part.

    It seems most plugins have a lot of extra’s that can be done away with if the proper code is applied.

    You are absolutely correct though about looking through the code of the plugin. I will try that as well. Thanks so much Joy.

    bward

    (@bward)

    I have had some more time to think over my design and research this. I am still having a difficult time here and would love some help.

    I am sure I am far off the mark, but I will post my code anyway. I started to break things with the second set I believe, I must be getting close! hah

    function xyz () {
    	if ($post_type() && $requested_url()) {
    		$requested_url .= $_SERVER['REQUEST_URI'];
    	}
    }
    
    if ( ( is_home() && $query->is_main_query() ) || is_feed() ) {
            $query->set( 'post_type', array( 'post', 'example-one', 'example-two', 'example-three') );
     return $query;
    }
    add_filter( 'pre_get_posts', 'xyz' );
    function xzy () {
    	if ($post_type() && $requested_url()) 
    		$requested_url .= $_SERVER['REQUEST_URI'];
    	}
    
    if ( ( is_home() && $query->is_main_query() ) || is_feed() ) {
            $query->set( 'post_type', array( 'post', 'example-one', 'example-two', 'example-tree') );
     return $query;
    }
    add_filter( 'pre_get_posts', 'xyz' );
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.