WordPress가 빈 post_in 배열에서 쿼리 결과를 표시하는 이유는 무엇입니까?
다음과 같은 것이 있습니다.WP_Query
인수:
$posts = new WP_Query(array(
'post__in' => $postids,
'meta_key' =>'ratings_average',
'orderby'=>'meta_value_num',
'order' =>'DESC',
));
$postids는 다른 ID에서 가져온 ID 배열입니다.WP_Query
여기서의 문제는 $postid가 비어 있어도 Wordpress 루프에 게시물이 표시된다는 것입니다.$postids가 비어 있으면 게시물이 표시되지 않도록 하려면 어떻게 해야 합니까?
이 방법으로는 문제를 직접 해결할 수 없습니다.post__in
근데 왜 안 되는지 모르겠어
if(!empty($postids)){
$posts = new WP_Query(array(
'post__in' => $postids,
'meta_key' =>'ratings_average',
'orderby'=>'meta_value_num',
'order' =>'DESC',
));
} else {
//Do something else or nothing at all..
}
보다시피WP_Query
콜이 발생하는 것은$postids
값이 포함되어 있습니다.그렇지 않으면, 에 대한 콜은 발신되지 않습니다.WP_Query
쿼리가 0개의 투고를 반환한 경우와 마찬가지로 루프는 발생하지 않습니다.
전술한 바와 같이 wp devs는 이것을 수정하고 싶지 않습니다.이렇게 하면 다음과 같이 비활성 ID 배열을 비워두지 않고 전달할 수 있습니다.
if(empty($postids)) {
$postids = ['issue#28099'];
}
$posts = new WP_Query(array(
'post__in' => $postids,
'meta_key' =>'ratings_average',
'orderby'=>'meta_value_num',
'order' =>'DESC',
));
나쁜 습관이라고?네, 어느 쪽인지는 모르겠지만...
WP_Query를 사용하여 흐름을 올바르게 유지합니다.다음과 같이 사용합니다.
$postIdArray = array(
1, 2, 3
);
$queryArgs = array(
'post_type' => 'any',
'post_status' => 'published',
'post__in' => ((!isset($postIdArray) || empty($postIdArray)) ? array(-1) : $postIdArray)
);
이렇게 하면 WP_Query 객체에 대해 코드를 작성할 수 있습니다.
예를 들어 다음과 같습니다.
$postIdArray = array(
1, 2, 3
);
$queryArgs = array(
'post_type' => 'any',
'post_status' => 'published',
'post__in' => ((!isset($postIdArray) || empty($postIdArray)) ? array(-1) : $postIdArray)
);
$postQuery = new \WP_Query($queryArgs);
$postCount = $postQuery->post_count;
$totalCount = $postQuery->found_posts;
방금 같은 문제가 발생했습니다.가장 좋은 방법은 어레이가 비어 있는지 확인하고 비활성 ID를 전달하는 것입니다.
if(empty($postids)){
$postids[]= 0;
}
쿼리 및 문제 해결 전에 추가해 주세요.
아마 당신은 끈적끈적한 게시물을 가지고 있을 거예요.이 경우 WordPress는 이러한 게시물을 쿼리에 추가합니다.
해결책은 다음과 같이 설정하는 것입니다.'ignore_sticky_posts' => 1
코드에 적용:
$posts = new WP_Query(array(
'post__in' => $postids,
'ignore_sticky_posts' => 1,
'meta_key' =>'ratings_average',
'orderby'=>'meta_value_num',
'order' =>'DESC',
));
언급URL : https://stackoverflow.com/questions/23247671/why-is-wordpress-showing-query-results-from-an-empty-post-in-array
'programing' 카테고리의 다른 글
React.js: 튜토리얼의 예가 작동하지 않습니다. (0) | 2023.03.17 |
---|---|
AngularJS : 1개의 Angular 방향에서 여러 서브 요소를 변환합니다. (0) | 2023.03.17 |
'node_modules/@testing-library/react/dist/pure.js'에서 'react-dom/client' 모듈을 찾을 수 없습니다. (0) | 2023.03.17 |
프로젝터 브라우저기다림은 기다리지 않다 (0) | 2023.03.17 |
각도 뒤에 jQuery 코드 실행JS가 HTML 렌더링을 완료합니다. (0) | 2023.03.17 |