Advanced query

Get posts from category AND tag:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$new_query['tax_query'] = array(
 
    array(
 
        'taxonomy' => 'category',
 
        'terms' => array('cat1'),
 
        'field' => 'slug',
 
    ),
 
    array(
 
        'taxonomy' => 'post_tag',
 
        'terms' => array('tag1'),
 
        'field' => 'slug',
 
    ),
 
);
 
query_posts($new_query);

Get posts, which are NOT IN category:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$new_query['tax_query'] = array(
 
    array(
 
        'taxonomy' => 'category',
 
        'terms' => array('cat1', 'tag1'),
 
        'field' => 'slug',
 
        'operator' => 'NOT IN',
 
    ),
 
);
 
query_posts($new_query);

Get posts from category OR tag:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$new_query['tax_query'] = array(
 
    'relation' => 'OR',
 
    array(
 
        'taxonomy' => 'category',
 
        'terms' => array('cat1'),
 
        'field' => 'slug',
 
    ),
 
    array(
 
        'taxonomy' => 'post_tag',
 
        'terms' => array('tag1'),
 
        'field' => 'slug',
 
    ),
 
);
 
query_posts($new_query);

Get posts from tag OR post-format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$new_query = wp_parse_args($query_string);
 
$new_query['tax_query'] = array(
 
    'tax_query' => array(
 
        'relation' => 'OR',
 
        array(
 
            'taxonomy' => 'post_tag',
 
            'terms' => array('tag1'),
 
            'field' => 'slug',
 
        ),
 
        array(
 
            'taxonomy' => 'post_format',
 
            'terms' => array('new-post-format'),
 
            'field' => 'slug',
 
        ),
 
    ),
 
) );
 
query_posts($new_query);

Leave a Comment