0

I have the following

  • custom post type: lesson
  • custom Taxonomy: subject
    • which contains terms like math, reading, etc.)

I have configured my page so that the user may filter the results by entering a taxonomy "subject" as a query variable:

http://www.myWebsiteURL.com/lessons&lc=study-skills

This works great for getting a single taxonomy category, however I am having difficulty getting ALL taxonomy categories in a single request (ie for an overview/unfiltered page).

Here is how I'm building my query:

<?php
    /* if URL query value is a valid category, get all lessons for that category.
       If it is not a valid category (or not category is provided), get ALL lessons. */

    $args = ["post-type"=>"lesson", "post-status" => "publish"];
    $validCats = ['study-skills','time-management','math','tutoring','reading','online-learning'];
    if ( isset($_GET['lc']) && in_array($_GET['lc'], $validCats) ) {
        $args["subject"] = $_GET['lc'];
    } else {
        $arg["subject"] = "*"; // ??? what goes here?
    }
    $lessons = new WP_Query($args);
?>
6
  • How does your URL looks for all taxonomy terms? Mar 13, 2015 at 16:30
  • Have you tried lc[]=study-kills&lc[]=math-is-great? That would be equal to form fields with a name of name="lc[]".
    – kaiser
    Mar 13, 2015 at 16:30
  • I'm using Advanced Custom Fields and Custom Post Types UI plugins to create my post types/taxonomy, if that's any help. I'm not sure exactly what you mean, @kaiser
    – tdc
    Mar 13, 2015 at 17:26
  • @PieterGoosen it should just be /lessons to get all the taxonomy terms. Thats what I need help figuring out
    – tdc
    Mar 13, 2015 at 17:27
  • And that gives you a 404 Mar 13, 2015 at 17:29

1 Answer 1

2

I would rather use a full proper tax_query here. The {tax} syntax is depreciated according to the docs

A tax_query is also better here as you will be dealing with an array of terms. When your URL is

http://www.myWebsiteURL.com/lessons

this means that all terms should be displayed. So this means that you need to use the full $validCats array as query terms.

With this in mind, I have slightly changed your code to the following: (CAVEAT: Untested, and I assume you are using term slugs here, so I have set the field parameter accordingly)

<?php
    /* if URL query value is a valid category, get all lessons for that category.
       If it is not a valid category (or not category is provided), get ALL lessons. */

    $validCats = ['study-skills', 'time-management', 'math', 'tutoring', 'reading', 'online-learning'];

    if ( isset( $_GET['lc'] ) && in_array( $_GET['lc'], $validCats ) ) {
        $terms = $_GET['lc'];
    } else {
        $terms = $validCats;
    }

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = [
        'post_type'   => 'lesson', 
        'post_status' => 'publish',
        'paged'       => $paged,
        'tax_query'   => [
            [
                'taxonomy' => 'subject',
                'field'    => 'slug',
                'terms'    => $terms
            ]
        ]
    ];

    $lessons = new WP_Query($args);
?>
6
  • This did the trick, thank you! One last question (wp newbie here :) ) -- how can I modify the WP_Query you've provided to allow for pagination?
    – tdc
    Mar 13, 2015 at 17:57
  • Glad it works :-). I have updated my code to include pagination. Just a note that I did not inclue, if you are going to use next_posts_link(), you should add $lessons->max_num_pages as second parameter to next_posts_link() Mar 13, 2015 at 18:03
  • 1
    Beautiful. Thanks for the tip. Pagination is working great!
    – tdc
    Mar 13, 2015 at 18:39
  • Great. Enjoy :-) Mar 13, 2015 at 18:40
  • Oh! One last thing --- do you know how I can integrate this with Jetpack plugin Infinite Scroll? I have it working for my main (default) query for blog posts, however I'm not sure how to get it working with this custom query.
    – tdc
    Mar 13, 2015 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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