Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP_查询和meta_查询通过Like获取值_Wordpress_Wildcard_Sql Like_Meta Key - Fatal编程技术网

Wordpress WP_查询和meta_查询通过Like获取值

Wordpress WP_查询和meta_查询通过Like获取值,wordpress,wildcard,sql-like,meta-key,Wordpress,Wildcard,Sql Like,Meta Key,我正在建立一个自定义搜索。 在这一点上,我试图得到一些帖子,其中搜索的文本应该被用作通配符,以便在2个meta_键内进行比较 下面是我的代码 <?php $args = ( array( 'post_type' => 'registration', 'meta_key'=>'rg_upload_video', 'meta_query' => array( 'relation' => '

我正在建立一个自定义搜索。 在这一点上,我试图得到一些帖子,其中搜索的文本应该被用作通配符,以便在2个meta_键内进行比较

下面是我的代码

<?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\u键的帖子返回为rg\u upload\u video。我需要在meta_keyrg_first_namerg_last_name中搜索文本的帖子


有人知道我哪里出错了吗?

试着运行下面的代码

<?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);

?>

您可以执行两个
WP\u查询
查询。在第一个选项中,选择具有
rg\u upload\u video
meta键的所有选项。然后执行另一个WP_查询,只搜索在第一个查询中获得的帖子。未测试,但它应该看起来像:

$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);

您是否尝试过删除
'meta\u key'=>'rg\u upload\u video',
?没有,因为这是第一个条件。Post应该有这个meta键,第二个条件是按姓名(名字/姓氏)过滤。我认为你不能在同一个
meta\u查询中同时执行
以及
。这是不对的。好吧,你不能把两个关系放在meta_查询中。我想,如果上面的查询不起作用,自定义SQL查询是唯一的方法。不,WP_查询就足够了。自定义SQL查询比不必要地运行2个WP_查询要好。如果你经常需要查询,你也可以使用瞬态来存储帖子ID