Wordpress get_posts: how to use this php function to build lists of posts

     

$posts = new WP_Query(array( "post__in" => $postids, "meta_key" =>"ratings_average", "orderby"=>"meta_value_num", "order" =>"DESC", ));$postids is an array of ids which is retrieved from another WP_Query. My problem here is that even if $postids is empty, Wordpress loop shows posts. How can I manage this that it shouldn"t show any post if $postids is empty.

Bạn đang xem: Wordpress get_posts: how to use this php function to build lists of posts


*

*

This isn"t directly fixing the issue with post__in but I don"t see why this wouldn"t work..

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..as you can see the WP_Query Call will only happen if $postids has value/s in it. if it doesn"t, then no điện thoại tư vấn is made to lớn WP_Query và the loop will just never happen, same as if your query returned 0 posts.


*

*

As noted, wp devs don"t want lớn fix this. Having said that, you could pass a non-empty array of invalid IDs, like this:

if(empty($postids)) $postids = <"issue#28099">;$posts = new WP_Query(array( "post__in" => $postids, "meta_key" =>"ratings_average", "orderby"=>"meta_value_num", "order" =>"DESC",));Bad practice you say ? Yeah, I am not sure from whose side though ...


*

To keep the flow correct with the WP_Query. Use it like this:

$postIdArray = array( 1, 2, 3 ); $queryArgs = array( "post_type" => "any", "post_status" => "published", "post__in" => ((!isset($postIdArray) || empty($postIdArray)) ? array(-1) : $postIdArray) );This way you will still be able lớn code against the WP_Query object.

Xem thêm: Những Hình Nền Thư Pháp Đẹp, Độc Đáo Và Phóng Khoáng, Tải 20 Hình Nền Thư Pháp Cho Điện Thoại

For example:

$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;
Just got the same problem, best thing is to lớn check if the array is empty then pass invalid ID khổng lồ it:

if(empty($postids)) $postids<>= 0;Add that before the query and the problem is solved.


Maybe you have some sticky posts. In this case WordPress will add those posts lớn your query.

The solution is to lớn set "ignore_sticky_posts" => 1. Applying that to lớn your code:

$posts = new WP_Query(array( "post__in" => $postids, "ignore_sticky_posts" => 1, "meta_key" =>"ratings_average", "orderby"=>"meta_value_num", "order" =>"DESC",));
Thanks for contributing an answer to lớn Staông xã Overflow!

Please be sure to lớn answer the question. Provide details và chia sẻ your research!

But avoid

Asking for help, clarification, or responding khổng lồ other answers.Making statements based on opinion; baông xã them up with references or personal experience.

Xem thêm: 5 Công Cụ Kiểm Tra Tốc Độ Load Trang Web, Top 20 Công Cụ Kiểm Tra Tốc Độ Website Miễn Phí

To learn more, see our tips on writing great answers.


Post Your Answer Discard

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy và cookie policy


Not the answer you're looking for? Browse other questions tagged php wordpress or ask your own question.


WordPress WP_Query: Display custom post type based on custom meta value, & also order on another custom meta value
site thiết kế / biểu tượng logo © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev2021.5.25.39367


Your privacy

By clicking “Accept all cookies”, you agree Staông chồng Exchange can store cookies on your device and discthất bại information in accordance with our Cookie Policy.


Chuyên mục: Kinh doanh online

Dành cho bạn