• Recently we created a custom taxonomy called alphabetical_letter, which includes all 26 letters of the alphabet. Therein, in each taxonomy are all posts with different categories.

    Now we would like to display the music reviews with the letter N displayed under an archive page with the letter N. For all of the letters we have now created a custom loop and custom taxonomy page calling the taxonomy and showing only the posts for the said category.

    So if we would visit …/alphabetical_letter-n/ that page now shows the posts in category music review which have the keyword N.

    However… we would also like to do this for other categories like news, live reviews and so forth, which are also categorised under that letter n but the …/alphabetical_letter-n/ is naturally taken now with the custom loo.

    We are looking into the option of creating child taxonomies,
    …/alphabetical_letter-n/albumreview …/alphabetical_letter-n/livereview
    but find that we cant load the posts of a parent taxonomy into a child taxonomy.

    we were hoping that somebody could give us some advice on this matter.
    can this be solved by creating templates for archive pages ?

    thanks,

    Nick

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    First some clarification of terminology is in order. I’m pretty sure you are looking at child terms within a taxonomy, not child taxonomies. The former is a built in feature, the latter requires much custom coding. Some may think the distinction is trivial, but it’s important that all involved can be sure they are all speaking of the same things.

    I’m not sure why you’re having trouble, but the solution is almost certainly solved by hooking into the “pre_get_posts” action. You can set the post_type and tax_query query vars there to build incredibly complex queries. The values assigned are the same as the WP_Query arguments. The main challenge is ensuring you are only modifying the intended query, as ALL post queries pass through this action.

    Checking $wp_query->is_main_query() is one thing to verify. Checking for certain values in $wp_query->query_vars array should be enough to determine you are changing the right query.

    Thread Starter nessler

    (@nessler)

    Thanks for the info.
    The theme we bought and are customizing is quite complex to put it mildly.

    The page where we are currently developing is this “taxonomy-alphabetical_letter-a.php”, which is the parent letter in the taxonomy alphabetical letter, so yes technically not child taxonomies but terms.

    Would it be a correct way to proceed as follows:
    Create child terms for the taxonomy “alphabetical letter’ pages for all the letters,
    -A (id 441, 500 posts)
    -musicreviews-a (no posts)
    -livereviews-a (no posts)
    -…..
    -B (id 442, 400 posts)
    -musicreviews-a (no posts)
    -livereviews-a (no posts)
    -…..

    then create custom taxonomy pages like so:
    “taxonomy-alphabetical_letter-a-musicreviews-a.php”
    “taxonomy-alphabetical_letter-a-livereviews-b.php”

    and then try to adjust the loop accordingly per your instructions ?
    If you’d be so kind to point us in the right direction with the code below (if this is where the fix would be) we’d be on our way to try and overcome this issue.

    The current alternative I’m following through is creating custom archive pages which seems like a lot of work since I’m guessing this should be a fairly easy fix…

    the loop we are currently using looks like this,

    $args = array(
    'post_type' => 'post',
    'posts_per_page'         => '-1',
    'meta_key'			=> 'keyword',
    'orderby'			=> 'meta_value',
    'order'				=> 'ASC',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'alphabetical_letter',
            'field'    => 'term_id',
            'terms'    => array( '441' ), //this is by slug
        ),
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'musicreviews' ), //this is by id
        ),
    ),
    
    );
    
    // The Query
    query_posts( $args );
    
    // The Loop
    
    if (have_posts()) {
        while ( have_posts() ) : the_post();
            echo $td_template_layout->layout_open_element();
    
            if (class_exists($td_module_class)) {
                $td_mod = new $td_module_class($post);
                echo $td_mod->render();
            } else {
                td_util::error(__FILE__, 'Missing module: ' . $td_module_class);
            }
    
            echo $td_template_layout->layout_close_element();
            $td_template_layout->layout_next();
        endwhile; //end loop
        echo $td_template_layout->close_all_tags();
    
    } else {
        /**
         * no posts to display. This function generates the __td('No posts to display').
         * the text can be overwritten by the themplate using the global @see td_global::$custom_no_posts_message
         */
    
        echo td_page_generator::no_posts();
    }

    thanks for your insights, Nick.

    • This reply was modified 6 years, 7 months ago by nessler.
    Moderator bcworkz

    (@bcworkz)

    Once you’ve ironed out the bugs, your scheme could work, but it would be tragic for you to have to create 52+ templates unless there was no other choice. There must be another choice! You should try to work with what WP already does for you, then tweak it to meet your needs. Your scheme throws out what WP tries to do and reinvents everything.

    Unless each letter’s output is so incredibly unique that a unique template is required, it’s not a good plan. Maybe the different reviews need different templates, but even those can be wrapped into one template if the differences aren’t too large. I mean template differences only, the review posts themselves can be all over the map. Minor template differences can be managed with if/else statements.

    Depending on the desired output, it’s even conceivable the theme’s default archive.php or even index.php template could work, maybe with some modification. To get WP to help you out, it needs to understand the request’s URL structure. It does not recognize multiple term requests by default, you need to help it out with add_rewrite_rule(). It takes some effort to understand how to use this, but worth the effort, it’s a useful, powerful function. Just remember to visit the permalinks settings screen after making any rewrite changes.

    If done correctly, I believe WP will return the proper reviews without further modification. If not, we make adjustments through the “pre_get_posts” action. The output will probably be on the archive.php by default. When doing custom rewrites, I’m unsure if the normal template naming overrides will work. If you still need a custom template for the output, you may need to alter the template loading logic through the “template_include” filter.

    I have the feeling that I may have completely overwhelmed you at this point. It’s not as bad as it may sound. It’s better than making 52+ templates! Focus on the add_rewrite_rule() and forget the rest for now. This alone could get you far enough along that there’s no need for anything else. Even if not, whatever other issues arise can be dealt with. One step at a time!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Advice wanted on custom alphabet taxonomy with different categories’ is closed to new replies.