dscizpq790832708
2018-10-31 06:38
浏览 222
已采纳

Wordpress Post__not_在通过变量提供时不起作用

I have created a function and passing dynamically the comma separated values to wp_args. When i echo variable i get the exact as needed and passing statci gies result but while passing variable name, I do not get results.

$excludepages1 = "12,14";
$excludepages2 = "'".implode("','", explode(",", $excludepages1))."'";
$excludepages = str_replace('"', '', $excludepages2); 

Now If i echo $excludepages I get '12','14' But when i pass here

$children = get_posts( array(
        'post_type' =>'page',
        'posts_per_page' =>-1,
        'post_parent' => $post_id,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__not_in' => array($excludepages)));

I do not get any result and if instead of variable i pass '12','14', I get results, Can you please help?

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

我创建了一个函数并动态地将逗号分隔值传递给wp_args。 当我回显变量时,我得到精确的所需并传递statci gies结果,但在传递变量名称时,我没有得到结果。

  $ excludepages1 =“12,14”;  
 $ excludepages2 =“'”。implode(“','”,explode(“,”,$ excludepages1))。“'”; 
 $ excludepages = str_replace('“','',$ excludepages2);  
   
 
 

现在如果我回显 $ excludepages 我得到'12','14' 但是当我 传递到这里

  $ children = get_posts(array(
'post_type'=>'page',
'posst_per_page'=> -1,
'post_parent  '=> $ post_id,
'orderby'=>'menu_order',
'order'=>'ASC',
'posst _not_in'=> array($ excludepages))); 
 <  / code>  
 
 

我没有得到任何结果,如果不是变量我通过'12','14',我得到结果,你能帮忙吗? ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douleijiang8111 2018-10-31 06:58
    已采纳

    Ref: https://developer.wordpress.org/reference/functions/get_posts/

    $excludepages1 = "12,14";
    $excludepages = explode(",", $excludepages1);
    

    An exploded array can be used directly.

    "exclude" param is suggested in the docs. So, I used instead of "post__not_in".

    $children = get_posts( array(
                    'post_type' =>'page',
                    'posts_per_page' =>-1,
                    'post_parent' => $post_id,
                    'orderby' => 'menu_order',
                    'order' => 'ASC',
                    'exclude' => $excludepages)
                );
    
    点赞 评论
  • dpndp64206 2018-10-31 07:05

    Doing it the way you are you are creating an array with a 0 index and value of "12,14." What you are doing is passing a string "12, 14" to the first index of an array. What you want to do is pass two integers into an array. So if you print array($excludepages) the way you're doing it you'll see

    Array
    (
        [0] => 12,14
    )
    

    What you want is

    Array
    (
        [0] => 12
        [1] => 14,
    )
    

    I'm not sure what you're trying to do with the implodes, explodes, and str_replace, but you'll want to define your array using something like:

    $excludepages = array(12, 14); // Notice, no quotes in the array declaration.
    

    or

    $excludepages = array();
    $excludepages[] = 12;
    $excludepages[] = 14;
    

    Then in the get_posts it would look like:

    $children = get_posts( array(
            'post_type' =>'page',
            'posts_per_page' =>-1,
            'post_parent' => $post_id,
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'post__not_in' => $excludepages) );
    
    点赞 评论
  • dongxie5698 2018-10-31 08:09

    Problem is not with the post__not_in argument. It takes array, not a comma separated string:

    'post__not_in' (array) An array of post IDs not to retrieve. Note: a string of comma- separated IDs will NOT work.

    More details: https://developer.wordpress.org/reference/classes/wp_query/parse_query/

    Your $excludepages returns string(9) "'12','14'".

    You should update it like:

    $excludepages1 = "12,14";
    $excludepages = explode(",", $excludepages1);
    
    $children = get_posts( array(
        'post_type' =>'page',
        'posts_per_page' =>-1,
        'post_parent' => $post_id,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__not_in' => $excludepages)
    );
    

    In above code, $excludepages will return:

    array(2) {
      [0]=> string(2) "12"
      [1]=> string(2) "14"
    }
    
    点赞 评论

相关推荐 更多相似问题