WordPress.org

Ready to get started?Download WordPress

Forums

Listing Posts from Mulitiple Categories (2 posts)

  1. DamianCunniff
    Member
    Posted 7 years ago #

    Hi All,

    Forgive me as I'm new to this whole wordpress thing.

    I'm consuming an RSS feed from one of my WordPress blogs and I couldn't get it to return posts from multiple categories. Basically if I have the categories: "Business", "Internet", and "Sales" I'd like to return an RSS feed of posts in the "Business" and the "Sales" category.

    I maneged to track down the code in the classes.php file that makes selections based on categories fed into the URI. It looks like this:


    // Category stuff for nice URIs

    global $cache_categories;
    if ('' != $q['category_name']) {
    $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
    $q['category_name'] = sanitize_title(basename($cat_paths));
    $cat_paths = explode('/', $cat_paths);
    foreach($cat_paths as $pathdir)
    $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);

    $all_cat_ids = get_all_category_ids();
    $q['cat'] = 0; $partial_match = 0;
    foreach ( $all_cat_ids as $cat_id ) {
    $cat = get_category($cat_id);
    if ( $cat->fullpath == $cat_path ) {
    $q['cat'] = $cat_id;
    break;
    } elseif ( $cat->category_nicename == $q['category_name'] ) {
    $partial_match = $cat_id;
    }
    }

    //if we don't match the entire hierarchy fallback on just matching the nicename
    if (!$q['cat'] && $partial_match) {
    $q['cat'] = $partial_match;
    }

    $tables = ", $wpdb->post2cat, $wpdb->categories";
    $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
    $whichcat = " AND (category_id = '" . $q['cat'] . "'";
    $whichcat .= get_category_children($q['cat'], " OR category_id = ");
    $whichcat .= ")";
    }

    And I modified it as follows:

    // Category stuff for nice URIs

    global $cache_categories;
    if ('' != $q['category_name']) {
    $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
    $q['category_name'] = sanitize_title(basename($cat_paths));
    $cat_paths = explode('/', $cat_paths);

    // Do this outside the loop so we only get it one time
    $all_cat_ids = get_all_category_ids();

    // Modified this loop iterate once for each category passed in.
    foreach($cat_paths as $pathdir)
    {
    $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);

    //$q['cat'] = 0; $partial_match = 0;
    //Changed to use a local variable instead of the query string 'cat'
    $category_id = 0; $partial_match = 0;
    foreach ( $all_cat_ids as $cat_id )
    {
    $cat = get_category($cat_id);
    if ( $cat->fullpath == $cat_path ) {
    $category_id = $cat_id;
    break;
    } elseif ( $cat->category_nicename == $q['category_name'] ) {
    $partial_match = $cat_id;
    }
    }

    //if we don't match the entire hierarchy fallback on just matching the nicename
    if (!$category_id && $partial_match) {
    $category_id = $partial_match;
    }

    //if there is already something in the $q['cat'] then we'll append a comma and
    //our new value
    if ('' != $q['cat'])
    {
    $q['cat'] .= ',';
    }
    $q['cat'] .= $category_id;
    }

    $tables = ", $wpdb->post2cat, $wpdb->categories";
    $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) ";
    //$whichcat = " AND (category_id = '" . $q['cat'] . "'";
    //changed this line to a "in" statement
    $whichcat = " AND (category_id in (" . $q['cat'] . ")";
    //Not sure what this next lines does... probably something important...
    //$whichcat .= get_category_children($q['cat'], " OR category_id = ");
    $whichcat .= ")";
    }

    It looks like the original code was intended to do what my modification does, but it may have been broken. In the original code if you pass multiple categories in it only uses the last category that is there. This seems to occur because of the structure of this for loop:

    foreach($cat_paths as $pathdir)
    $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir);

    Now I've made these changes and I don't know how they affect other bits of the application and I've screwed myself come upgrade time.

    Should I submit this stuff for inclusion? Am I wrong about the original intent of the devs? Is this not the way the app is intended to function?

    Thanks
    -Damian

  2. Phil2006
    Member
    Posted 7 years ago #

    In english please :P what is your problem, or try posting on a forum rather then the support section, more people use the Forums!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags