duanluan2047
2014-01-21 18:51
浏览 23
已采纳

合并/混合2个查询if和条件语句为真

I have this wp query...

$downloads = new WP_Query(array(
    'post_type' => 'download',
    'paged' => $paged,
    'posts_per_page' => 20
));


But I want to add this to the query if my $user_admin condition is true...

if ($user_admin)
    $downloads = new WP_Query(array(
        'meta_query' => array(
            array(
                'key' => 'download_access_rules',
                'value' => 'genpo',
                'compare' => 'NOT IN'
            )
        )
    )
);


So I run this it seems to break my loop, but not cause a fatal error...

$downloads = new WP_Query(array(
    'post_type' => 'download',
    'paged' => $paged,
    'posts_per_page' => 20
));

if ($user_admin) {
    $downloads = new WP_Query(array(
        'meta_query' => array(
            array(
                'key' => 'download_access_rules',
                'value' => 'genpo',
                'compare' => 'NOT IN'
            )
        )
    ));
}

OK my question is essentially this... How do I blend the two $downloads variables if the $user_admin condition equals true.

But the fastest and correct method of actually going about doing this as my method does not work.

图片转代码服务由CSDN问答提供 功能建议

我有这个wp查询......

  $ downloads  = new WP_Query(array(
'post_type'=>'download',
'paged'=> $ paged,
'stovers_per_page'=> 20 
)); 
  <  / pre> 
 
 


但是如果我的 $ user_admin 条件为真,我想将其添加到查询中...

  if($ user_admin)
 $ downloads = new WP_Query(array(
'meta_query'=&gt; array(
 array)
'key'=&gt;  ;'download_access_rules',
'值'=&gt;'genpo',
'比较'=&gt;'NOT IN'
)
)
)
); 
   
 
 


所以我运行它似乎打破了我的循环,但没有造成致命的错误...... \ n

  $ downloads = new WP_Query(array(
'post_type'=&gt;'download',
'paged'=&gt; $ paged,
'posrs_per_page'=&gt; 20 
)  ); 
 
if($ user_admin){
 $ downloads = new WP_Query(array(
'meta_query'=&gt; array(\  n数组(
'key'=&gt;  'download_access_rules',
'value'=&gt;  'genpo',
'比较'=&gt;  'NOT IN'
)
)
)); 
} 
   
 
 

好的我的问题基本上就是这个......我如何将两者混合在一起 如果 $ user_admin 条件等于true,则 $ downloads 变量。

但实际执行此操作的最快且最正确的方法是 方法不起作用。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqphg40600 2014-01-21 19:10
    已采纳

    From looking at your code it looks like you could merge the array together and then create the new WP_Query object. From your description I understand that you are saying that you want the queries to be blended into one and not the results of the query blended into one.

    $args = array(
        'post_type' => 'download',
        'paged' => $paged,
        'posts_per_page' => 20
    );
    
    if ($user_admin) {
        $args = array_merge($args, array(
            'meta_query' => array(
                array(
                    'key' => 'download_access_rules',
                    'value' => 'genpo',
                    'compare' => 'NOT IN'
                )
            )
        ));
    }
    
    $downloads = new WP_Query($args);
    

    I also was wondering you indicated that your current code seems to break your loop. Exactly what is happening with your first code example. Are you getting a blank page or simply not having any articles returned?

    Another thing to note when checking if a user is an administrator you can also use is_admin() instead of $user_admin.

    Function Reference/is admin

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doudundian9558 2014-01-21 18:59

    Try:

    $query = array(
        'post_type' => 'download',
        'paged' => $paged,
        'posts_per_page' => 20
    );
    
    
    if ($user_admin) {
        $query2 = array(
            'meta_query' => array(
                array(
                    'key' => 'download_access_rules',
                    'value' => 'genpo',
                    'compare' => 'NOT IN'
                )
            )
        );
    
       $query = array_merge($query, $query2);
    }
    
    $downloads = new WP_Query($query);
    

    Hope this helps!

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题