0

我正在构建自定义搜索。在这一点上,我试图获取应将搜索到的文本用作通配符以在 2 个 meta_key 内进行比较的帖子。

下面是我的代码,

<?php $args = (

    array(
        'post_type' => 'registration',
        'meta_key'=>'rg_upload_video',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'rg_first_name',
                'value' => $s,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'rg_last_name',
                'value' => $s,
                'compare' => 'LIKE'
            )
        ),
        'posts_per_page' => 12,
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
    );

        $loop = new WP_Query($args);

?>

结果,它只是返回所有具有Meta_keyrg_upload_video的帖子。我需要在 meta_key rg_first_namerg_last_name中搜索过文本的帖子。

有谁知道我要去哪里错了?

4

2 回答 2

-1

你可以做两个WP_Query查询。在第一个中,您选择所有具有rg_upload_video元键的内容。然后你做另一个 WP_Query 只搜索在第一个查询中获得的帖子。未经测试,但应该如下所示:

$args = array(
    'post_type' => 'registration',
    'posts_per_page' => -1,
    'meta_key'=>'rg_upload_video',
);

$videos = new WP_Query($args);

# get list of posts containing meta key rg_upload_video
$post_ids = wp_list_pluck( $videos->posts, 'ID' );

# You then do another WP_Query to get the posts having postmeta first name or last nav_menu_description

$args = array(
    'post_type' => 'registration',
    'post__in' => $post_ids,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'rg_first_name',
            'value' => $s,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'rg_last_name',
            'value' => $s,
            'compare' => 'LIKE'
        )
    ),
    'posts_per_page' => 12,
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);

$loop = new WP_Query($args);
于 2014-01-12T07:09:11.070 回答
-2

尝试运行以下代码。

<?php $args =  array(
        'post_type' => 'registration',
        'meta_query' => array(
            'relation' => 'AND',
            array(
              'relation' => 'OR',
              array(
                'key' => 'rg_first_name',
                'value' => $s,
                'compare' => 'LIKE'
              ),
              array(
                'key' => 'rg_last_name',
                'value' => $s,
                'compare' => 'LIKE'
              )
            ),
            array(
               'key' => 'rg_upload_video',
               'compare' => 'EXISTS' 
            )
        ),
        'posts_per_page' => 12,
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
    );

        $loop = new WP_Query($args);

?>
于 2014-01-09T09:57:43.233 回答