WordPress.org

Ready to get started?Download WordPress

Forums

CPT-onomies: Using Custom Post Types as Taxonomies
[resolved] RSS Feed parameters (27 posts)

  1. Jeff
    Member
    Posted 1 year ago #

    Absolutely love this plugin. I'm trying to determine how to accomplish the following functionality and I'm not sure if its just done through URL params or via a custom RSS feed template.

    I have a custom post type called "schools" which is CPT-onomies enabled and linked to the built in "posts" type. The RSS feed for a particular school should pull all posts assigned to that particular post type "school".

    Please let me know if you need any additional information regarding the implementation.

    Posts on theme template for this school: http://thelensnola.org/school/lycee-francais/

    Empty RSS feed for this school: http://thelensnola.org/school/lycee-francais/feed

    http://wordpress.org/extend/plugins/cpt-onomies/

  2. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    You basically need to hook into the query for the RSS feed so you can add a tax query.

    Add the following action to your code, probably your functions.php:

    NOTE: if you print out the query, and you're not in a development environment, then you'll want to wrap the code in some logic so only you can see what's happening.

    Also, the feed pages are severely cached so you might have to refresh A LOT between code changes OR empty your cache every time.

    add_action( 'pre_get_posts', 'my_website_pre_get_posts' );
    function my_website_pre_get_posts( &$query ) {
    
       // There's a 'feed' query variable that lets you
       // know you're running a feed so you can only run
       // your code during a feed.
       if ( $feed = get_query_var( 'feed' ) ) {
    
          // uncomment to print the query so you
          // can see what parameters are set
          /*echo "<pre>";
          print_r( $query );
          echo "</pre>";*/
    
          // You should also have a query variable for
          // 'schools' set to whatever the school name is
          if ( $school = get_query_var( 'schools' ) ) {
    
             // define tax query
             $query->tax_query->queries[] = array(
                'taxonomy' => 'schools',
                'terms' => array( $school ),
                'include_children' => 1,
                'field' => 'slug',
                'operator' => 'IN'
                );
    
          }
    
       }
    
    }
  3. Jeff
    Member
    Posted 1 year ago #

    That makes sense. I'm just not sure where I'm pulling the school slug from. The 'schools' query variable isn't showing up in the global query object.

    Do I need to modify the URL or do I need to add rewrite rules to make the query recognize the school from the URL?

  4. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    How do you have this feed URL work? Did you add_rewrite_rule()?

  5. Jeff
    Member
    Posted 1 year ago #

    I did not. I just added "feed" at the end of the URL to get the post-specific feed.

  6. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Can you show me what the $query looks like?

  7. Jeff
    Member
    Posted 1 year ago #

    Something else must be going on, because it appears the query thinks its on the products page (we are running the WP e-Commerce plugin).

    URL: http://lensnola.staging.wpengine.com/school/lycee-francais/feed/

    Response:

    WP_Query Object
    (
        [query_vars] => Array
            (
                [post_status] => publish
                [post_parent] => 0
                [order] => ASC
                [post_type] => wpsc-product
                [pagename] => products-page
                [orderby] => ID
                [error] =>
                [m] => 0
                [p] => 0
                [subpost] =>
                [subpost_id] =>
                [attachment] =>
                [attachment_id] => 0
                [name] =>
                [static] =>
                [page_id] => 0
                [second] =>
                [minute] =>
                [hour] =>
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] =>
                [tag] =>
                [cat] =>
                [tag_id] =>
                [author_name] =>
                [feed] =>
                [tb] =>
                [paged] => 0
                [comments_popup] =>
                [meta_key] =>
                [meta_value] =>
                [preview] =>
                [s] =>
                [sentence] =>
                [fields] =>
                [menu_order] =>
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
            )
    
        [tax_query] =>
        [meta_query] =>
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] =>
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] =>
        [is_preview] =>
        [is_page] => 1
        [is_archive] =>
        [is_date] =>
        [is_year] =>
        [is_month] =>
        [is_day] =>
        [is_time] =>
        [is_author] =>
        [is_category] =>
        [is_tag] =>
        [is_tax] =>
        [is_search] =>
        [is_feed] =>
        [is_comment_feed] =>
        [is_trackback] =>
        [is_home] =>
        [is_404] =>
        [is_comments_popup] =>
        [is_paged] =>
        [is_admin] =>
        [is_attachment] =>
        [is_singular] => 1
        [is_robots] =>
        [is_posts_page] =>
        [is_post_type_archive] =>
        [query_vars_hash] => 6c0c4c8bcede69bf39f0c714b5b6a57c
        [query_vars_changed] =>
        [thumbnails_cached] =>
        [query] => Array
            (
                [post_status] => publish
                [post_parent] => 0
                [order] => ASC
                [post_type] => wpsc-product
                [pagename] => products-page
                [orderby] => ID
            )
    
        [queried_object] => WP_Post Object
            (
                [ID] => 38080
                [post_author] => 1
                [post_date] => 2013-03-21 14:47:58
                [post_date_gmt] => 2013-03-21 19:47:58
                [post_content] => [productspage]
                [post_title] => Products Page
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] =>
                [post_name] => products-page
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2013-04-03 10:08:58
                [post_modified_gmt] => 2013-04-03 15:08:58
                [post_content_filtered] =>
                [post_parent] => 0
                [guid] => http://lensnola.staging.wpengine.com/products-page/
                [menu_order] => 0
                [post_type] => page
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )
    
        [queried_object_id] => 38080
        [is_product] => 1
    )
  8. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    You may have to do add_rewrite_rule() so you can define the parameters.

    Would probably be something like:

    add_action( 'wp_loaded', 'my_website_rewrite_rules' );
    function my_website_rewrite_rules() {
       add_rewrite_rule( '^school/([^\/]+)/feed/?$','index.php?feed=feed&schools=$matches[1]', 'top' );
    }
  9. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    You may have to do add_rewrite_rule() so you can define the parameters.

    Would probably be something like:

    add_action( 'wp_loaded', 'my_website_rewrite_rules' );
    function my_website_rewrite_rules() {
       add_rewrite_rule( '^school/([^\/]+)/feed/?$','index.php?feed=feed&schools=$matches[1]', 'top' );
    }
  10. Jeff
    Member
    Posted 1 year ago #

    Not sure why WP e-Commerce is messing with the query, but I deactivated it and it appears everything is working as expected except the actual feed. If I remove my die statement and let the request complete, it redirects back to the school page (lensnola.staging.wpengine.com/school/lycee-francais/).

    Query after my pre_get_posts hook with rewrite rules enabled:

    WP_Query Object
    (
        [query_vars] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
                [error] =>
                [m] => 0
                [p] => 0
                [post_parent] =>
                [subpost] =>
                [subpost_id] =>
                [attachment] =>
                [attachment_id] => 0
                [static] =>
                [pagename] =>
                [page_id] => 0
                [second] =>
                [minute] =>
                [hour] =>
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] =>
                [tag] =>
                [cat] =>
                [tag_id] =>
                [author_name] =>
                [tb] =>
                [paged] => 0
                [comments_popup] =>
                [meta_key] =>
                [meta_value] =>
                [preview] =>
                [s] =>
                [sentence] =>
                [fields] =>
                [menu_order] =>
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
            )
    
        [tax_query] => stdClass Object
            (
                [queries] => Array
                    (
                        [0] => Array
                            (
                                [taxonomy] => school
                                [terms] => Array
                                    (
                                        [0] => lycee-francais
                                    )
    
                                [include_children] => 1
                                [field] => slug
                                [operator] => IN
                            )
    
                    )
    
            )
    
        [meta_query] =>
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] =>
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 1
        [is_preview] =>
        [is_page] =>
        [is_archive] =>
        [is_date] =>
        [is_year] =>
        [is_month] =>
        [is_day] =>
        [is_time] =>
        [is_author] =>
        [is_category] =>
        [is_tag] =>
        [is_tax] =>
        [is_search] =>
        [is_feed] => 1
        [is_comment_feed] => 1
        [is_trackback] =>
        [is_home] =>
        [is_404] =>
        [is_comments_popup] =>
        [is_paged] =>
        [is_admin] =>
        [is_attachment] =>
        [is_singular] => 1
        [is_robots] =>
        [is_posts_page] =>
        [is_post_type_archive] =>
        [query_vars_hash] => 7832e8aa476657087351138dffd158ec
        [query_vars_changed] =>
        [thumbnails_cached] =>
        [query] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
            )
    
    )
  11. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    I forgot about the archive thing for CPT-onomies. Try this:

    and, FYI, you shouldn't need the 'pre_get_posts' action if you're doing the rewrite rule.

    add_action( 'wp_loaded', 'my_website_rewrite_rules' );
    function my_website_rewrite_rules() {
       add_rewrite_rule( '^school/([^\/]+)/feed/?$','index.php?feed=feed&schools=$matches[1]&cpt_onomy_archive=1', 'top' );
    }
  12. Jeff
    Member
    Posted 1 year ago #

    That doesn't seem to work. I commented out the pre_get_posts hook and the feed is still empty.

    In functions.php (note I uncommented the pre_get_posts to show where I'm printing the query; results below)

    add_action( 'pre_get_posts', 'largo_child_pre_get_posts' );
    function largo_child_pre_get_posts( &$query ) {
    
       if ( $feed = get_query_var( 'feed' ) ) {
    
    	  // uncomment to print the query so you
          // can see what parameters are set
        print_r( $query );
    	  die('testing');
    
          // You should also have a query variable for
          // 'schools' set to whatever the school name is
          if ( $school = get_query_var( 'school' ) ) {
    
          	// define tax query
             $query->tax_query->queries[] = array(
                'taxonomy' => 'school',
                'terms' => array( $school ),
                'include_children' => 1,
                'field' => 'slug',
                'operator' => 'IN'
                );
          }
       }
    }
    
    add_action( 'wp_loaded', 'largo_child_rewrite_rules' );
    function largo_child_rewrite_rules() {
       add_rewrite_rule( '^school/([^\/]+)/feed/?$','index.php?feed=feed&schools=$matches[1]&cpt_onomy_archive=1', 'top' );
    }

    Results of print_r (before adding the tax_queries):

    WP_Query Object
    (
        [query_vars] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
                [error] =>
                [m] => 0
                [p] => 0
                [post_parent] =>
                [subpost] =>
                [subpost_id] =>
                [attachment] =>
                [attachment_id] => 0
                [static] =>
                [pagename] =>
                [page_id] => 0
                [second] =>
                [minute] =>
                [hour] =>
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] =>
                [tag] =>
                [cat] =>
                [tag_id] =>
                [author_name] =>
                [tb] =>
                [paged] => 0
                [comments_popup] =>
                [meta_key] =>
                [meta_value] =>
                [preview] =>
                [s] =>
                [sentence] =>
                [fields] =>
                [menu_order] =>
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
            )
    
        [tax_query] =>
        [meta_query] =>
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] =>
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 1
        [is_preview] =>
        [is_page] =>
        [is_archive] =>
        [is_date] =>
        [is_year] =>
        [is_month] =>
        [is_day] =>
        [is_time] =>
        [is_author] =>
        [is_category] =>
        [is_tag] =>
        [is_tax] =>
        [is_search] =>
        [is_feed] => 1
        [is_comment_feed] => 1
        [is_trackback] =>
        [is_home] =>
        [is_404] =>
        [is_comments_popup] =>
        [is_paged] =>
        [is_admin] =>
        [is_attachment] =>
        [is_singular] => 1
        [is_robots] =>
        [is_posts_page] =>
        [is_post_type_archive] =>
        [query_vars_hash] => 7832e8aa476657087351138dffd158ec
        [query_vars_changed] =>
        [thumbnails_cached] =>
        [query] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
            )
    
    )
    testing
  13. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Is the post type called 'school' or 'schools'?

  14. Jeff
    Member
    Posted 1 year ago #

    'school'

    I noticed that too, so I changed the rewrite rule to the following, but it still shows up empty.

    add_action( 'wp_loaded', 'largo_child_rewrite_rules' );
    function largo_child_rewrite_rules() {
       add_rewrite_rule( '^school/([^\/]+)/feed/?$','index.php?feed=feed&school=$matches[1]&cpt_onomy_archive=1', 'top' );
    }
  15. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    what's the query after the change?

  16. Jeff
    Member
    Posted 1 year ago #

    WP_Query Object
    (
        [query_vars] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
                [error] =>
                [m] => 0
                [p] => 0
                [post_parent] =>
                [subpost] =>
                [subpost_id] =>
                [attachment] =>
                [attachment_id] => 0
                [static] =>
                [pagename] =>
                [page_id] => 0
                [second] =>
                [minute] =>
                [hour] =>
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] =>
                [tag] =>
                [cat] =>
                [tag_id] =>
                [author_name] =>
                [tb] =>
                [paged] => 0
                [comments_popup] =>
                [meta_key] =>
                [meta_value] =>
                [preview] =>
                [s] =>
                [sentence] =>
                [fields] =>
                [menu_order] =>
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
            )
    
        [tax_query] =>
        [meta_query] =>
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] =>
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 1
        [is_preview] =>
        [is_page] =>
        [is_archive] =>
        [is_date] =>
        [is_year] =>
        [is_month] =>
        [is_day] =>
        [is_time] =>
        [is_author] =>
        [is_category] =>
        [is_tag] =>
        [is_tax] =>
        [is_search] =>
        [is_feed] => 1
        [is_comment_feed] => 1
        [is_trackback] =>
        [is_home] =>
        [is_404] =>
        [is_comments_popup] =>
        [is_paged] =>
        [is_admin] =>
        [is_attachment] =>
        [is_singular] => 1
        [is_robots] =>
        [is_posts_page] =>
        [is_post_type_archive] =>
        [query_vars_hash] => 7832e8aa476657087351138dffd158ec
        [query_vars_changed] =>
        [thumbnails_cached] =>
        [query] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
            )
    
    )
    testing
  17. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Try adding '&post_type=any' to the rewrite rule and show me the query.

  18. Jeff
    Member
    Posted 1 year ago #

    I don't see any changes.

    WP_Query Object
    (
        [query_vars] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
                [error] =>
                [m] => 0
                [p] => 0
                [post_parent] =>
                [subpost] =>
                [subpost_id] =>
                [attachment] =>
                [attachment_id] => 0
                [static] =>
                [pagename] =>
                [page_id] => 0
                [second] =>
                [minute] =>
                [hour] =>
                [day] => 0
                [monthnum] => 0
                [year] => 0
                [w] => 0
                [category_name] =>
                [tag] =>
                [cat] =>
                [tag_id] =>
                [author_name] =>
                [tb] =>
                [paged] => 0
                [comments_popup] =>
                [meta_key] =>
                [meta_value] =>
                [preview] =>
                [s] =>
                [sentence] =>
                [fields] =>
                [menu_order] =>
                [category__in] => Array
                    (
                    )
    
                [category__not_in] => Array
                    (
                    )
    
                [category__and] => Array
                    (
                    )
    
                [post__in] => Array
                    (
                    )
    
                [post__not_in] => Array
                    (
                    )
    
                [tag__in] => Array
                    (
                    )
    
                [tag__not_in] => Array
                    (
                    )
    
                [tag__and] => Array
                    (
                    )
    
                [tag_slug__in] => Array
                    (
                    )
    
                [tag_slug__and] => Array
                    (
                    )
    
            )
    
        [tax_query] =>
        [meta_query] =>
        [post_count] => 0
        [current_post] => -1
        [in_the_loop] =>
        [comment_count] => 0
        [current_comment] => -1
        [found_posts] => 0
        [max_num_pages] => 0
        [max_num_comment_pages] => 0
        [is_single] => 1
        [is_preview] =>
        [is_page] =>
        [is_archive] =>
        [is_date] =>
        [is_year] =>
        [is_month] =>
        [is_day] =>
        [is_time] =>
        [is_author] =>
        [is_category] =>
        [is_tag] =>
        [is_tax] =>
        [is_search] =>
        [is_feed] => 1
        [is_comment_feed] => 1
        [is_trackback] =>
        [is_home] =>
        [is_404] =>
        [is_comments_popup] =>
        [is_paged] =>
        [is_admin] =>
        [is_attachment] =>
        [is_singular] => 1
        [is_robots] =>
        [is_posts_page] =>
        [is_post_type_archive] =>
        [query_vars_hash] => 7832e8aa476657087351138dffd158ec
        [query_vars_changed] =>
        [thumbnails_cached] =>
        [query] => Array
            (
                [feed] => feed
                [school] => lycee-francais
                [post_type] => school
                [name] => lycee-francais
            )
    
    )
    testing
  19. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    Quick question: have you flushed your rewrite rules at all? You're supposed to do that every time you tweak a rewrite rule.

  20. Jeff
    Member
    Posted 1 year ago #

    Yep. From the Rewrite Rules Inspector plugin: http://screencast.com/t/BVAFQHEqK

  21. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    You may have to bring 'pre_get_posts' back into the mix so you can remove the 'post_type' and the 'name' query variable.

    This means you can remove '&post_type=any' from the rewrite rule.

    add_action( 'pre_get_posts', 'my_website_pre_get_posts' );
    function my_website_pre_get_posts( &$query ) {
    
       // There's a 'feed' query variable that lets you
       // know you're running a feed so you can only run
       // your code during a feed.
       if ( $feed = get_query_var( 'feed' ) ) {
    
          // You should also have a query variable for
          // 'schools' set to whatever the school name is
          if ( $school = get_query_var( 'school' ) ) {
    
             unset( $query[ 'post_type' ] );
             unset( $query[ 'name' ] );
    
          }
    
       }
    
    }
  22. Jeff
    Member
    Posted 1 year ago #

    I think caching was working against me. I don't need the pre_get_posts hook anymore. Simply removing &post_type=any from the rewrite rule seems to have solved the problem.

    Here is the working code:

    add_action( 'wp_loaded', 'largo_child_rewrite_rules' );
    function largo_child_rewrite_rules() {
       add_rewrite_rule( '^school/([^\/]+)/feed/?$','index.php?feed=feed&school=$matches[1]&cpt_onomy_archive=1', 'top' );
    }

    Proof: http://lensnola.staging.wpengine.com/school/lycee-francais/feed/

  23. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    It looks like you might be messing with your site because it's throwing a fatal error... but if you say it's working, then hooray!

    Darn that caching! I've had several instances where caching was the problem. When it helps, it helps... but when it gets in the way? It usually means you spent forever figuring that out!

  24. Jeff
    Member
    Posted 1 year ago #

    I was/still kind of am, but that link should work now.

  25. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    It does! Good deal!

  26. Jeff
    Member
    Posted 1 year ago #

    Thanks for your help!

  27. Rachel Carden
    Member
    Plugin Author

    Posted 1 year ago #

    No problem. Glad I could help. :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic