网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

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

GG网络技术分享 2025-03-18 16:12 2


问题描述:

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结果,但在传递变量名称时,我没有得到结果。</ p>

  $ excludepages1 =“12,14”;  

$ excludepages2 =“\'”。implode(“\',\'”,explode(“,”,$ excludepages1))。“\'”;

$ excludepages = str_replace(\'“\',\'\',$ excludepages2);

</ code> </ pre>

现在如果我回显 $ excludepages </ code>我得到\'12\',\'14\'</ code>

但是当我 传递到这里</ p>

  $ children = get_posts(array(

\'post_type\'=&gt;\'page\',

\'posst_per_page\'=&gt; -1,

\'post_parent \'=&gt; $ post_id,

\'orderby\'=&gt;\'menu_order\',

\'order\'=&gt;\'ASC\',

\'posst _not_in\'=&gt; array($ excludepages)));

< / code> </ pre>

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

</ div>

网友观点:

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)

);

###

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

###

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\\\"

}

标签:

提交需求或反馈

Demand feedback