如何通过WP_Query实现按名称和城市搜索WordPress Agent自定义文章类型
Got it, let's work through this. You need to search for Agents while filtering by the City taxonomy assigned to their linked Properties—since City is tied to Property, not Agent directly, we have to bridge those two post types in the query. Here's a complete, secure solution tailored to your setup:
Step-by-Step Adjusted Code
if (isset($_POST['search']) && !empty($_POST['search'])) { // Sanitize user input to avoid security risks $agent_search_term = sanitize_text_field($_POST['search']); $city_filter = isset($_POST['city']) ? sanitize_text_field($_POST['city']) : ''; // Add hooks to join Property data and filter by City add_filter('posts_join', 'link_agents_to_property_cities'); add_filter('posts_where', 'filter_agents_by_assigned_city'); add_filter('posts_distinct', 'remove_duplicate_agent_results'); // Build the core WP_Query arguments $args = array( 'post_type' => 'agent', 's' => $agent_search_term, 'posts_per_page' => -1, // Show all matching agents; adjust as needed 'post_status' => 'publish' ); // Run the query $agent_query = new WP_Query($args); // Clean up hooks so they don't break other queries later remove_filter('posts_join', 'link_agents_to_property_cities'); remove_filter('posts_where', 'filter_agents_by_assigned_city'); remove_filter('posts_distinct', 'remove_duplicate_agent_results'); // Output results if ($agent_query->have_posts()) { while ($agent_query->have_posts()) { $agent_query->the_post(); // Display your Agent content here (example) echo '<h3>' . get_the_title() . '</h3>'; echo '<p>' . get_the_content() . '</p>'; } wp_reset_postdata(); } else { echo '<p>No agents match your search criteria.</p>'; } } // Join Agent posts with their linked Properties and City taxonomy data function link_agents_to_property_cities($join) { global $wpdb; // Join Property posts where the Agent custom field matches the Agent's ID $join .= " INNER JOIN {$wpdb->postmeta} pm ON {$wpdb->posts}.ID = pm.meta_value "; $join .= " INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID AND p.post_type = 'property' AND p.post_status = 'publish' "; // Join City taxonomy tables to access city data $join .= " INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id "; $join .= " INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'city' "; $join .= " INNER JOIN {$wpdb->terms} t ON tt.term_id = t.term_id "; return $join; } // Add city filter to the query (matches city slug by default) function filter_agents_by_assigned_city($where) { global $wpdb; $city_filter = isset($_POST['city']) ? sanitize_text_field($_POST['city']) : ''; if (!empty($city_filter)) { // Use t.name instead of t.slug if you want to match the city's display name $where .= " AND t.slug = '{$wpdb->esc_like($city_filter)}' "; } return $where; } // Ensure each Agent only appears once (even if they have multiple Properties in the same city) function remove_duplicate_agent_results($distinct) { return 'DISTINCT'; }
Key Notes to Consider
- Custom Field Assumption: This code assumes your Property's "Agent" custom field stores the Agent post ID (not the Agent name). This is the most reliable way to link post types—if you're storing names instead, adjust the join condition to
pm.meta_value = {$wpdb->posts}.post_title(but IDs are strongly recommended). - City Matching: The code filters by city slug (e.g.,
nyfor New York). If you want to match the city's display name instead, swapt.slugwitht.namein thefilter_agents_by_assigned_cityfunction. - Security: All user input is sanitized to prevent SQL injection and XSS vulnerabilities—never skip this step!
- Cleanup: We remove the hooks after the query runs to avoid interfering with other WordPress queries on the page.
内容的提问来源于stack exchange,提问作者AZinkey
相关产品推荐
相关产品推荐

