Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to filter WordPress posts by a taxonomy and between parameter? Consider a mobile website with "memory size" taxonomy, I need to show posts that have more than 256 mb memory and less than 768 mb. Is it possible through WP_Query class or "posts_where" filter? I know that post_meta is better for this purpose, but website has been built before.

share|improve this question
add comment

1 Answer

Tax Query Limits

A Taxonomy Query in WordPress supports the following three arguments for the operator parameter: IN, NOT IN and AND. So it basically can't do what you're trying to do. Not even with advanced (tax_query) Queries.

Meta Query and possibilities

You'll want to move your concept a bit and work with post meta fields/data. The WP_Query uses the WP_Meta_Query, which has some advantages over the WP_Tax_Query. The main one (for this question) is, that it supports more advanced relations through the compare arguments and you can as well sort by it: 'orderby' => 'meta_value_num' and 'orderby' => 'meta_value'.

(string) compare - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Default value is '='.

- Quote from the Meta Query part of the WP_Query documentation @Codex wp dot org.

So you could (should) switch your taxonomies that you want to order and filter/search by to become custom fields/meta data.

Not possible? Stepped taxonomies for the rescue.

As I already explained on top of the answer, there's an IN operator for taxonomy queries. So what you could do is a call to get_terms() Source: queryposts.com and grab all terms that are in your range and then build an array of taxons/terms that serves as your "limiters".

// Set "min/max"-limits:
$min = 256;
$max = 768;
// Get all terms in that taxonomy
$memory = get_terms( array( 'memory_size' ) );

// Container for our values
$mem_range = array();
// Check if we got an error
if ( ! is_wp_error( $memory ) )
{
    foreach ( $memory as $val )
    {
        // Convert to absolute integer for comparison
        $val = absint( $val );

        // Add to stack
        $val >= $min
        AND $val <= $max
            AND $mem_range[] = $val;
    }
}
// Debug
else
{
    // Output only for admin users and others that have that cap
    current_user_can( 'manage_options' )
        AND print $memory->get_error_message();
}

// Setup the basic arguments
$args = array( 'post_type' => 'hardware' );
// Check if we got some terms in range
if ( ! empty( $mem_range ) )
{

    $args['tax_query'] = array( array(
        'taxonomy' => 'memory_size',
        'field'    => 'slug',
        'terms'    => $mem_range,
        'operator' => 'IN'
    ) );
}

// Grab the results
$hardware_search = new WP_Query( $args );

Conclusion

While this might not be the best idea for high performance websites as you're doing two queries for a single task, it may fulfill ones needs. I'd still recommend switching over to meta data for more advanced, single queries.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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