如何在WordPress的WP_Query查询中排除指定的3个Agent类型文章ID
How to Exclude Specific Agent Posts from Your WP_Query
Hey Claudio, glad to help you tweak that WP_Query! Excluding those three agent posts by ID is super straightforward with WordPress's built-in post__not_in parameter. Here's exactly how to update your code:
Modified Code Snippet
// Initialize the query args with the exclusion parameter $agents_query = array( 'post_type' => 'agent', 'posts_per_page' => $number_of_posts, 'paged' => $paged, 'orderby' => 'rand', 'post__not_in' => array(5707, 5782, 5755) // This line excludes your target agent posts ); // Apply your existing sorting function $agents_query = inspiry_agents_sort_args( $agents_query ); // Initialize the query $agent_listing_query = new WP_Query( $agents_query );
Key Details:
- The
post__not_inparameter accepts an array of post IDs you want to exclude from the query results. - Since your
inspiry_agents_sort_args()function likely only adjusts sorting-related parameters (likeorderbyororder), it won't interfere with thepost__not_infilter—your excluded posts will stay excluded after sorting is applied. - Double-check that 5707, 5782, and 5755 are indeed valid IDs for your
agentcustom post type (you mentioned they are, so this is just a quick sanity check!).
That's all you need to do—this should immediately remove those three agents from your query results.
内容的提问来源于stack exchange,提问作者Claudio Xerez
相关产品推荐
相关产品推荐

