I'm having a page on which all my custom post types are displayed. I now need to add another to the existing ones:

$args = array(
    'post_type' => 'bkroadkill',
    'post_status'    => 'publish, future',
    'posts_per_page'    => '15'

if ( 'POST' == $_SERVER['REQUEST_METHOD']  && isset($_POST['filterform']) && wp_verify_nonce($_POST['filterform'],'filterform') )
        if (isset($_POST['own']) && !empty($_POST['own'])){
            $args['author'] = get_current_user_id();
        if (isset($_POST['comments']) && !empty($_POST['comments'])){
            $args['orderby'] = 'comment_count' ;
query_posts( $args );

This is working perfeclty fine. What I want to do now is add these filters

  • Filter by selecting one or more categories from wp_dropdown_categories of my custom taxonomy
  • Filter by meta value which is already set

For all of the filters except the category simple checkboxes are used. I just check if they are set and when they are I change my string.

My attempts for the other 2 filters failed:

    if (isset($_POST['cat']) && !empty($_POST['cat'])){
        $args['roadkill_category'] = $_POST['cat'];

As I am very new to wordpress even this simple stuff is confusing me


I was able to create the filter for my meta value now:

if (isset($_POST['bestimmung']) && !empty($_POST['bestimmung'])){   
        $args['meta_query'] = array(array(
                                            'key'     => 'bestimmung',
                                            'value'   => 'nein',
                                            'compare' => 'IS'

So I'm just missing the filter for the custom taxonomy.

  • 1
    you can't mix querystring and array arguments, see WP_Query for proper argument formatting.
    – Milo
    Jan 30 '14 at 16:37
  • I tried it with WP_Query before and wasn't able to access any of my post meta in the loop then. Also it would be easy to combine the string like this if several options are picked. How could i achieve that using WP_Query?
    – 4ndro1d
    Jan 30 '14 at 16:44
  • Could you change the code to use the WP_Query then paste here? So we can figure out why you can't get your post meta.
    – 1fixdotio
    Jan 30 '14 at 16:49
  • I will give it a try now with using $args array codex.wordpress.org/Function_Reference/query_posts see last example. Can I use array_push to add values from my form?
    – 4ndro1d
    Jan 30 '14 at 16:52
  • I updated my question, I guess I am heading in the right direction.
    – 4ndro1d
    Jan 30 '14 at 17:03

To do the taxonomy query in your case, the code would be like this:

if (isset($_POST['cat']) && !empty($_POST['cat'])){
    $args['tax_query'] = array(
            'taxonomy' => 'roadkill_category',
            'field' => 'id', // or slug
            'terms' => $_POST['cat'],
            'operator' => 'IN'

If you have further questions about tax query, please refer to the Codex.

  • 1
    Works like a charm. Except you forgot a semicolon ;) Thanks a lot for this. And should I use get_posts() instead of query_posts()? I tried it and I get very strange resuluts (only one of 3 posts coming up and no meta data available)
    – 4ndro1d
    Jan 30 '14 at 17:42
  • Okay I'm wrong, I'm not able to select multiple categories from my dropdown. I just used to jQuery to make my select accept multiple choices: jQuery("#cat").attr( 'multiple', 'multiple' );
    – 4ndro1d
    Jan 30 '14 at 17:58

Looks like you fixed the first issue, the second one is in the same vein, as is described here.

$args['tax_query'] = array( array(
        'taxonomy'  => 'custom-taxonomy',
        'field'     => 'id',
        'terms'     => $term_id,
        'operator'  => 'IN'

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.