From ce5e3bb4724f7241770dabe41d2192c99ebdd599 Mon Sep 17 00:00:00 2001 From: "Hanson.xyz Dev" Date: Sun, 30 Nov 2025 19:19:11 -0600 Subject: [PATCH] Fix agent listing query to work without meta fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous WP_Query with meta_query required agent_order to exist, which excluded agents that hadn't been edited since the field was added. Changed to simpler approach: fetch all published agents with get_posts(), filter disabled in PHP, sort by order (defaulting to 10) then alphabetically. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- wp-content/themes/homeproz/archive-agent.php | 64 ++++++++++---------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/wp-content/themes/homeproz/archive-agent.php b/wp-content/themes/homeproz/archive-agent.php index 05887ff7..53e973ea 100644 --- a/wp-content/themes/homeproz/archive-agent.php +++ b/wp-content/themes/homeproz/archive-agent.php @@ -12,31 +12,33 @@ if (!defined('ABSPATH')) { get_header(); -// Query active agents, ordered by agent_order then title -$agents_query = new WP_Query([ +// Query all published agents, then filter disabled and sort by order +$all_agents = get_posts([ 'post_type' => 'agent', 'posts_per_page' => -1, 'post_status' => 'publish', - 'meta_query' => [ - 'relation' => 'OR', - // Agent is not disabled - [ - 'key' => 'agent_disabled', - 'value' => '1', - 'compare' => '!=', - ], - // Or agent_disabled field doesn't exist yet - [ - 'key' => 'agent_disabled', - 'compare' => 'NOT EXISTS', - ], - ], - 'meta_key' => 'agent_order', - 'orderby' => [ - 'meta_value_num' => 'ASC', - 'title' => 'ASC', - ], ]); + +// Filter out disabled agents and add sorting data +$agents_data = []; +foreach ($all_agents as $agent) { + $disabled = get_field('agent_disabled', $agent->ID); + if ($disabled) continue; + + $order = get_field('agent_order', $agent->ID); + $agents_data[] = [ + 'post' => $agent, + 'order' => $order ? (int) $order : 10, // Default to 10 if not set + ]; +} + +// Sort by order, then by title +usort($agents_data, function($a, $b) { + if ($a['order'] !== $b['order']) { + return $a['order'] - $b['order']; + } + return strcmp($a['post']->post_title, $b['post']->post_title); +}); ?>
@@ -51,24 +53,26 @@ $agents_query = new WP_Query([
- have_posts()) : ?> +
- have_posts()) : $agents_query->the_post(); - $agent_id = get_the_ID(); + ID; $agent_phone = get_field('agent_phone', $agent_id); $agent_email = get_field('agent_email', $agent_id); $agent_title = get_field('agent_title', $agent_id); $agent_short_bio = get_field('agent_short_bio', $agent_id); + $agent_permalink = get_permalink($agent_id); // Get featured image $agent_photo_id = get_post_thumbnail_id($agent_id); $agent_photo_url = $agent_photo_id ? wp_get_attachment_image_url($agent_photo_id, 'medium_large') : ''; ?> - +

No team members to display at this time.

- -