1

I have been struggling with Taxonomies and Post Types for a while and could really use help understanding what would be the best implementation for my situation.

Have a look here: http://shaulzofef.com/netivyah/resources/audio-lessons/

I setup a few post types; Books, Magazines, Articles and Audiolessons. I setup a few Taxonomies: Language, Author, Teacher. Teacher is associated to one post type only, Audiolessons, but Language is associated to all post types. I made a submenu on the audio page that has English, Hebrew and Russian at parent level and underneath a list of teachers in each language.

I have a few problems: 1 - Some teachers teach in more than one language, so if I click on Joseph under English how will I make it list only the posts with taxonomy term Joseph AND taxonomy term English? 2 - When I click on English it lists the English Audiolessons AND other posts associated with taxonomy term English (articles, books and so on). How would I list only the Audiolessons?

I realize I can code a Wordpress query for posts related to post type and taxonomy, however that will require a file for each combination..

I could create separate taxonomies for each post type, 'Audio-Language' for instance, then I could easily list them, but that doesn't seem right.. then what if I do want an option to list all English resources together. Should I create hierarchical taxonomies instead?.. I'm getting confused. Any help much appreciated.

8
  • Your page isn't loading. I don't know if this will help you, but I found it helpful when I had multiple taxonomies: thereforei.am/2011/10/28/… May 18, 2014 at 17:07
  • Thank you @helgatheviking. That indeed helped. Now audiolesson/language/english/teacher/josephshulam/ gives me what I want. but audiolesson/language/english still lists all english posts, including not audio lessons. It is not filtering by post type. What am I doing wrong? I would appreciate if you have a look at my plugin file here
    – Shaul
    May 19, 2014 at 9:30
  • You can add code to your answer instead of linking off-site. May 19, 2014 at 12:26
  • It's just a long piece of code, I didn't want to mess the page...
    – Shaul
    May 19, 2014 at 12:30
  • Sorry, I'm not going to look over your entire plugin. If you are posting your entire plugin the odds are that you aren't asking a specific enough question and that you aren't posting just the relevant code. I would try rephrasing your question. May 19, 2014 at 12:35

1 Answer 1

0

If you want to filter your audio lessons post archive you could borrow from the article I linked you to and add a custom rewrite rule:

function wpa_add_rewrite_rules() {
    global $wp_rewrite;

    $new_rules = array(
        'audio-lessons/language/(.+)/?$' => 'index.php?post_type=audio-lesson&language=' . $wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'wpa_add_rewrite_rules' );

After re-saving your permalinks, something like http://example.com/audio-lessons/language/english would show all the English audio lessons.

However, I am not sure the URL you provided is a post type archive and not a custom page template.

If it is a page template and you are running multiple Wp_Query() calls you could register a new query variable and build your tax_query up from that.

function wpa_add_new_query_vars($vars) {   
    $vars[] = 'language'; // name of variable you want to add       
    return $vars;
}
add_filter( 'query_vars', 'wpa_add_new_query_vars', 10, 1 );

You may not even need this part as I think the query var is registered when you register the taxonomy.

Then you could query posts like so:

$args = array(
    'post_type' => 'audio-lesson'
);

if( $lang = get_query_var( 'language' ) ){
    $args['tax_query'] = array( array( 
            'taxonomy' => 'language',
            'field' => 'slug',
            'terms' => '$lang'
        ));
}

$my_posts = new WP_Query( $args );

I can't really test this and the tax_query can sometimes trip me up with its array of arrays so I'm not 100% sure it will work.

Then anywhere you wanted to make a link to a specific language section you could use add_query_arg()

2
  • Thank you so much! Your custom rewrite rule worked! And the reason I was getting audio lessons of all languages before was because I had a query_posts in my archive-audiolesson.php page that overwrote the query. My bad. Thanks again :)
    – Shaul
    May 19, 2014 at 15:25
  • And to build the menu by URL's I used the Relative Menu Item Plugin
    – Shaul
    May 19, 2014 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.