简体   繁体   中英

Trying to filter Wordpress query by custom taxonomy terms

I'm trying to filter a Wordpress query based on terms in a custom taxonomy. I'm using the Essential Grid plugin to show posts from a custom post type 'offer'.

I'd like to grab values from the 'f' URL parameter and then use those values as the terms for filtering the query.

I'm not sure why my code isn't working. When I try to save it, I get a message that it will cause a fatal error.

Can someone with more PHP experience help me out? I've gone through the code multiple times and haven't seen the issue. Tested several variations as well.

function eg_mod_query($query, $grid_id){

if($grid_id == 3) {
 $f = $_GET['f'];
    $filters = explode(', ', $f);
$args = array(
    'post_type' => 'offer',
    'tax_query' => array(
        array(
            'taxonomy' => 'offer_taxonomy',
            'field'    => 'slug',
            'terms'    => array($filters),
        ),
    ),
);
$query = new WP_Query( $args );
    }
    return $query;
}
$filters = explode(', ', $f);

Line above is already in an array format. You should not change that to

array($filters).

In args var which changes the format.

I think this should work:

function eg_mod_query($query, $grid_id){

if($grid_id == 3) {

    $f = $_GET['f'];
    $filters = explode(', ', $f);
    $args = array(
        'post_type' => 'offer',
        'tax_query' => array(
            array(
                'taxonomy' => 'offer_taxonomy',
                'field'    => 'slug',
                'terms'    => $filters,
            ),
        ),
    );
    $query = new WP_Query( $args );
}

return $query;

}

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM