2016-09-17 5 views
1

나는 내 wordpress에 대한 무작위 링크를 거의 생성하지 않지만, mysql에 부하가 많이 걸릴 것으로 예상하므로 주문을 사용하고 싶지 않다. 링크 아래에 잘 작동하지만 난 ORDERBY 랜드에게여기서 word 절의 절

$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_arg = array(
     'numberposts' => $wprpi_value['post'], 
     'post__not_in' => array(get_the_ID()), 
     'where ID <' => get_the_ID(), 
     'orderby'  => 'ID', 
     'post_status' => 'publish', 
    ); 

를 사용하고자하지만이 작동하지 않습니다. WordPress는 orderby id에 해당하는 5 개의 링크를 내림차순으로 표시하므로 테이블의 마지막 링크 5 개만 나열합니다.

하지만 현재의 ID보다 5 개의 링크를 생성하고 싶습니다.

예 : 사용자가 페이지/10000에 있으면 생성 된 링크는 9999,9998,9997,9996,9995이고 사용자가/100 페이지에서 생성 된 링크는 99,98,97,96,95이지만 wordpress는 where 절을 무시하고 항상 동일한 링크를 생성합니다. orderby desc 제한 5 where 절을 고려하지 않고

+0

당신은 ID가 <' => 인 get_the_ID(),'''가''for codex '처럼'''처럼하지 않습니다. –

+0

@PrafullaKumarSahu는 어떻게 설명 할 수 있습니까? 저는 WordPress에 처음입니다 – Steeve

+0

전체 스 니펫을 게시 할 수 있습니까? 여기에 작성한 쿼리/기능을 의미합니까? ID를 사용하고 있다면 왜 게시 작업을 수행하는지 쿼리를 작성하는 이유는 무엇입니까? –

답변

0

다음과 같이 사용자 정의 where where 조건을 WordPress 쿼리에서 사용할 수 있습니다.

참고 : 귀하의 경우

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

에서 사용 get_the_ID() 대신 (215)이 의지 suppress_filters가 인수에 전달하는 모든 get_posts() 쿼리에 적용했다.

사용자 정의 post_type이있는 경우 게시 유형에 ckeck이 있거나 단일 페이지가있는 경우 해당 페이지 만 추가하십시오.