• Hi!

    I am not sure if this has been posted/resolved before–my Google-fu isn’t working on this one. This is probably because I can’t figure out the best way to phrase the search. Anyway, here is my problem:

    I have a site I am rebuilding. It is my faculty site where I post my course materials: homework assignments, class announcements, tutorials, etc. for each class I am teaching (or have taught). At the moment, I have the ready-made WP post type (no custom post types), categories, and a few custom taxonomies. The categories describe the types of courseware posted (assignments, announcements, tutorials, etc.). One of the taxonomies is a hierarchical one called “course”. The terms in it are the course codes for the class I teach (or have taught). Terms for past classes are moved to be sub-terms (?) of a term called “Archives”. The other taxonomies aren’t relevant for this.

    I have a menu that lists the categories (the types of courseware). The menu is used for each course. Right now, if a user clicks the link to one of the menu items (let’s say Assignments) the default action is to show a generic category.php template file that displays all of the posts for that category. So, for example, clicking Assignments will show all of the posts in the category assignments. What I want to have happen is to show only the posts in a given category that also have the course term. As an example, if I were on the home page looking at the course snapshot (shows latest post for a given course term that’s not archived) for course IMD100 and clicked the menu link for Assignments, I want to retrieve a list of all assignments for course term IMD100 only.

    I hope this makes sense. I am figuring there is something to do with sending additional query vars or custom URL, but I am at a loss as to where to start and what to look for. Any help is greatly appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • did you ever solve this?

    Thread Starter littleberry

    (@littleberry)

    More or less.

    What I ended up doing was get a list of the menu items that listed the categories (Assignments, Syllabus, etc.). Then I looped through each menu item. For each menu item, I added a query argument for the course tax term using add_query_arg() to the menu item’s url property. Finally, I output the menu as I wanted it to display. It might not be the most elegant solution, but it worked remarkably well. The category was filtered by the course tax term. That’s the more part. The less part is that only the most recent posts for a given category are displayed, per the settings in the admin. I didn’t catch that until I had more assignments than my setting was set to display. I either need to paginate or force all the posts to display. Or at least all the posts for a given quarter and paginate the rest.

    Here is the code I used:

    $course_resources_menu_items = wp_get_nav_menu_items( 'xx' );
    <?php foreach ( $course_resources_menu_items as $item ) {
    	$title = $item->title;
    	$url = add_query_arg( 'course', $term->slug, $item->url );
    	echo '<li><a href="' . $url . '" title="' . $title . '">' . $title . '</a></li>';
    }; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to filter posts in category by taxonomy term?’ is closed to new replies.