php 如何以编程方式获取与 WordPress 中的搜索查询匹配的帖子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9961192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:59:28  来源:igfitidea点击:

How to programmatically fetch posts matching a search query in WordPress?

phpwordpress

提问by Alan Bellows

In my plugin code I would like to perform a WP_Query(or similar) which returns all posts matching a given query string, as if the user typed that same string into the WordPress search form. Perhaps I'm just being dense, but I cannot seem to find a way to do so. I would expect to have a special parameter for WP_Query, such as matching, but I see no evidence of one.

在我的插件代码中,我想执行一个WP_Query(或类似的)返回与给定查询字符串匹配的所有帖子,就好像用户在 WordPress 搜索表单中输入了相同的字符串一样。也许我只是很密集,但我似乎无法找到这样做的方法。我希望有一个特殊的参数WP_Query,例如matching,但我看不到任何证据。

I'll start going through the WordPress codebase to see how it's done internally, and I'll post the answer here if I find it. I just thought someone might happen to know offhand.

我将开始浏览 WordPress 代码库,看看它是如何在内部完成的,如果找到答案,我会在这里发布。我只是想有人可能碰巧知道。

回答by gradyetc

Passing a query variable of "s" to WP_Querywith a search term will filter post results by search term:

将“s”的查询变量传递给WP_Query搜索词将按搜索词过滤帖子结果:

$query_args = array( 's' => 'disquiet' );
$query = new WP_Query( $query_args );

The corresponding SQL WHEREclause generated by this query looks like this:

WHERE此查询生成的相应 SQL子句如下所示:

AND (((wp_posts.post_title LIKE '%disquiet%') OR (wp_posts.post_content LIKE '%disquiet%')))

Default search includes the wildcards as shown above, which is most likely what you're looking for. If you want an exact search, you can also pass a query var of "exact" => true.

默认搜索包括如上所示的通配符,这很可能是您要查找的内容。如果您想要精确搜索,您还可以传递"exact" => true.

For the details see the get_postsmethod of WP_Queryin wp-includes/query.php.

有关细节请参阅get_posts的方法WP_QueryWP-包括/ query.php

回答by Aram Kocharyan

I use this in my plugin:

我在我的插件中使用它:

$query = new WP_Query(array(
    'post_type' => 'any',
    'suppress_filters' => TRUE,
    'posts_per_page' => '-1'
));

foreach ($query->posts as $post) {
    // ...
}

post_typeis needed if you're going to work with custom post types. suppress_filterswill prevent the content from being formatted if you need to analyse it. posts_per_pagewill return all posts, not the default per page.

post_type如果您要使用自定义帖子类型,则需要。suppress_filters如果您需要对其进行分析,它将阻止内容被格式化。posts_per_page将返回所有帖子,而不是每页的默认值。

回答by mrphpguru

This is a simpler and easier way to do search:

这是一种更简单、更容易的搜索方式:

$query = "
        SELECT      *
        FROM        $wpdb->posts
        WHERE       $wpdb->posts.post_title LIKE '$param2%'
        AND         $wpdb->posts.post_type = 'wp_exposants'
        ORDER BY    $wpdb->posts.post_title ";
 $wpdb->get_results($query);

回答by msigman

Something like this?

像这样的东西?

// Check the query variable is available
if(!$wp_query) global $wp_query; // If not, global it so it can be read from

// Your custom args
$args = array( 'the_title' => $search_term );

// Merge the custom args with any for the query already
$args = array_merge( $args , $wp_query->query );

// Now do the query
query_posts( $args );

Or you could try this:

或者你可以试试这个:

$query = array (
    'the_title' => $search_term
);

$queryObject = new WP_Query($query);
// The Loop...

回答by marvin

I believe you are looking is this compare

我相信你正在寻找的是这个 compare

$args = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key' => 'color',
            'value' => 'blue',
            'compare' => 'LIKE'
        )
    )
 );

from wordpress documentation

来自wordpress 文档

compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default value is '='.