Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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
Php wordpress中的where子句_Php_Mysql_Wordpress - Fatal编程技术网

Php wordpress中的where子句

Php wordpress中的where子句,php,mysql,wordpress,Php,Mysql,Wordpress,我想为我的wordpress生成几个随机链接,但我不想使用order by rand,因为它会给mysql带来沉重的负载。下面的链接很好,但当我使用orderby rand时 $wprpi_arg = array( 'numberposts' => $wprpi_value['post'], 'post__not_in' => array(get_the_ID()), // 'where ID <' => get_the

我想为我的wordpress生成几个随机链接,但我不想使用order by rand,因为它会给mysql带来沉重的负载。下面的链接很好,但当我使用orderby rand时

$wprpi_arg = array(
        'numberposts'   => $wprpi_value['post'],
        'post__not_in'  => array(get_the_ID()),
//      'where ID <'   => get_the_ID(),
        'orderby'       => 'rand',
        'post_status'   => 'publish',
    );
$wprpi\u arg=数组(
'numberposts'=>$wprpi_值['post'],
'post\u not\u in'=>数组(获取\u ID()),

//'where ID您可以在wordpress查询中使用自定义where条件,如下所示

注意:在您的案例中,使用get_the_ID()而不是215

//Add Filter
add_filter( 'posts_where' , 'posts_where_for_ID' );
function posts_where_for_ID( $where ) {
    $where .= ' AND ID < '. 215;
    return $where;
}

//Create args
$args = array(
'numberposts'       => 5, //$wprpi_value['post']
'post__not_in'      => array(215),  
'orderby'           => 'ID',
'order'             => 'DESC',
'post_status'       => 'publish',
'suppress_filters'  => FALSE
);

//Get the posts
$desc_posts = get_posts($args);

//Store post ids
$p_ids = array();
foreach($desc_posts as $posts_) {
   $p_ids[] = $posts_->ID;
}

//Your ids are here
var_dump($p_ids);
//添加过滤器
添加_过滤器('posts_where','posts_where_for_ID');
函数posts\u where\u for\u ID($where){
$where.='和ID<'.215;
返回$where;
}
//创建参数
$args=数组(
'numberposts'=>5,/$wprpi_值['post']
'post__not_in'=>数组(215),
'orderby'=>'ID',
“订单”=>“描述”,
“发布状态”=>“发布”,
“抑制_过滤器”=>FALSE
);
//获得职位
$desc_posts=获取_posts($args);
//邮政编码
$p_id=array();
foreach($desc_posts as$posts_uu){
$p_ID[]=$posts_->ID;
}
//你的身份证在这里
var_dump($p_id);
这将应用于所有
get_posts()
查询,其中
suppress_filters
传入参数


如果您有自定义的post类型,则勾选post类型,或者如果您有单页,则只添加该页。

您可以在wordpress查询中使用自定义where条件,如下所示

注意:在您的案例中,使用get_the_ID()而不是215

//Add Filter
add_filter( 'posts_where' , 'posts_where_for_ID' );
function posts_where_for_ID( $where ) {
    $where .= ' AND ID < '. 215;
    return $where;
}

//Create args
$args = array(
'numberposts'       => 5, //$wprpi_value['post']
'post__not_in'      => array(215),  
'orderby'           => 'ID',
'order'             => 'DESC',
'post_status'       => 'publish',
'suppress_filters'  => FALSE
);

//Get the posts
$desc_posts = get_posts($args);

//Store post ids
$p_ids = array();
foreach($desc_posts as $posts_) {
   $p_ids[] = $posts_->ID;
}

//Your ids are here
var_dump($p_ids);
//添加过滤器
添加_过滤器('posts_where','posts_where_for_ID');
函数posts\u where\u for\u ID($where){
$where.='和ID<'.215;
返回$where;
}
//创建参数
$args=数组(
'numberposts'=>5,/$wprpi_值['post']
'post__not_in'=>数组(215),
'orderby'=>'ID',
“订单”=>“描述”,
“发布状态”=>“发布”,
“抑制_过滤器”=>FALSE
);
//获得职位
$desc_posts=获取_posts($args);
//邮政编码
$p_id=array();
foreach($desc_posts as$posts_uu){
$p_ID[]=$posts_->ID;
}
//你的身份证在这里
var_dump($p_id);
这将应用于所有
get_posts()
查询,其中
suppress_filters
传入参数


如果你有自定义的post类型,那么请勾选post类型,或者如果你有单页,那么只添加该页。

你不像
where ID@PrafullaKumarSahu那样做。你能解释一下为什么我是WordPress的新手吗?你可以发布完整的代码片段来表示你在这里编写的查询/函数吗?如果你有ID,请使用它来获取post对象,为什么正在进行查询?您可以使用
posts\u where
filter。请参阅@SCC上的文档。您可以在这里给出一个示例吗?您不像
where ID@PrafullaKumarSahu那样做。您可以解释一下,我是WordPress的新手。您可以发布完整的代码片段来表示您在此处编写的查询/函数吗?如果您有ID,请使用它来获取post对象为什么您正在进行查询?您可以使用
posts\u where
filter。请参阅@SCC上的文档。您能在这里给出一个示例吗?