$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - WP_Query by keyword OR post tag|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - WP_Query by keyword OR post tag

matteradmin8PV0评论

Is there any 'native' way to query by keyword or post tag?

If not, how can I write a GOOD SQL query to retrieve the data correctly?

Thanks in advance!

Is there any 'native' way to query by keyword or post tag?

If not, how can I write a GOOD SQL query to retrieve the data correctly?

Thanks in advance!

Share Improve this question asked Jun 9, 2017 at 19:51 Bruno RodriguesBruno Rodrigues 1851 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Depending what you want to achieve, the most simple way taken from Wordpress documentation looks like below.

Displays posts tagged with "bob", under "people" custom taxonomy:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
            'taxonomy' => 'people',
            'field'    => 'slug',
            'terms'    => 'bob',
        ),
    ),
);
$query = new WP_Query( $args );

Check the documentation link - you'll find more advanced examples.

Update:

If you'd like to query actually for both 'keyword' or 'tag'...

It's funny - I did some research and it turns out that it isn't so trivial in Wordpress.

The most clean looking, native solution I found uses three queries. The first two get the ids - one by tag and the other by keyword, and then the third make the final query.

$set1 = new WP_Query( array('
                    'fields'=> 'ids',
                    'post_type' => 'post',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'people',
                            'field'    => 'slug',
                            'terms'    => 'bob'
                        )
                    )
                )
);
$set2 = new WP_Query( array(
                    'fields'=>'ids',
                    'post_type' => 'post',
                    's' => 'News')
);

$combined_ids = array_merge($set1->posts, $set2->posts);
$combined_ids = array_unique($combined_ids);

$combines_sets = new WP_Query(array(
                            'post__in' => $combined_ids)
);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far