Fix agent listing query to work without meta fields
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 <noreply@anthropic.com>
This commit is contained in:
@@ -12,31 +12,33 @@ if (!defined('ABSPATH')) {
|
|||||||
|
|
||||||
get_header();
|
get_header();
|
||||||
|
|
||||||
// Query active agents, ordered by agent_order then title
|
// Query all published agents, then filter disabled and sort by order
|
||||||
$agents_query = new WP_Query([
|
$all_agents = get_posts([
|
||||||
'post_type' => 'agent',
|
'post_type' => 'agent',
|
||||||
'posts_per_page' => -1,
|
'posts_per_page' => -1,
|
||||||
'post_status' => 'publish',
|
'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);
|
||||||
|
});
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<main id="primary" class="site-main Agents_Archive">
|
<main id="primary" class="site-main Agents_Archive">
|
||||||
@@ -51,24 +53,26 @@ $agents_query = new WP_Query([
|
|||||||
|
|
||||||
<section class="agents-section">
|
<section class="agents-section">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<?php if ($agents_query->have_posts()) : ?>
|
<?php if (!empty($agents_data)) : ?>
|
||||||
<div class="agents-grid">
|
<div class="agents-grid">
|
||||||
<?php while ($agents_query->have_posts()) : $agents_query->the_post();
|
<?php foreach ($agents_data as $agent_item) :
|
||||||
$agent_id = get_the_ID();
|
$agent = $agent_item['post'];
|
||||||
|
$agent_id = $agent->ID;
|
||||||
$agent_phone = get_field('agent_phone', $agent_id);
|
$agent_phone = get_field('agent_phone', $agent_id);
|
||||||
$agent_email = get_field('agent_email', $agent_id);
|
$agent_email = get_field('agent_email', $agent_id);
|
||||||
$agent_title = get_field('agent_title', $agent_id);
|
$agent_title = get_field('agent_title', $agent_id);
|
||||||
$agent_short_bio = get_field('agent_short_bio', $agent_id);
|
$agent_short_bio = get_field('agent_short_bio', $agent_id);
|
||||||
|
$agent_permalink = get_permalink($agent_id);
|
||||||
|
|
||||||
// Get featured image
|
// Get featured image
|
||||||
$agent_photo_id = get_post_thumbnail_id($agent_id);
|
$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') : '';
|
$agent_photo_url = $agent_photo_id ? wp_get_attachment_image_url($agent_photo_id, 'medium_large') : '';
|
||||||
?>
|
?>
|
||||||
<article class="agent-card-item">
|
<article class="agent-card-item">
|
||||||
<a href="<?php the_permalink(); ?>" class="agent-card-link">
|
<a href="<?php echo esc_url($agent_permalink); ?>" class="agent-card-link">
|
||||||
<div class="agent-card-image">
|
<div class="agent-card-image">
|
||||||
<?php if ($agent_photo_url) : ?>
|
<?php if ($agent_photo_url) : ?>
|
||||||
<img src="<?php echo esc_url($agent_photo_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
|
<img src="<?php echo esc_url($agent_photo_url); ?>" alt="<?php echo esc_attr($agent->post_title); ?>">
|
||||||
<?php else : ?>
|
<?php else : ?>
|
||||||
<div class="agent-card-placeholder">
|
<div class="agent-card-placeholder">
|
||||||
<svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" aria-hidden="true">
|
<svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" aria-hidden="true">
|
||||||
@@ -84,7 +88,7 @@ $agents_query = new WP_Query([
|
|||||||
<p class="agent-card-title-label"><?php echo esc_html($agent_title); ?></p>
|
<p class="agent-card-title-label"><?php echo esc_html($agent_title); ?></p>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
<h2 class="agent-card-name"><?php the_title(); ?></h2>
|
<h2 class="agent-card-name"><?php echo esc_html($agent->post_title); ?></h2>
|
||||||
|
|
||||||
<?php if ($agent_short_bio) : ?>
|
<?php if ($agent_short_bio) : ?>
|
||||||
<p class="agent-card-bio"><?php echo esc_html($agent_short_bio); ?></p>
|
<p class="agent-card-bio"><?php echo esc_html($agent_short_bio); ?></p>
|
||||||
@@ -112,7 +116,7 @@ $agents_query = new WP_Query([
|
|||||||
</a>
|
</a>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
<a href="<?php the_permalink(); ?>" class="agent-action-btn agent-action-profile">
|
<a href="<?php echo esc_url($agent_permalink); ?>" class="agent-action-btn agent-action-profile">
|
||||||
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
|
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
|
||||||
<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
|
<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
|
||||||
<circle cx="12" cy="7" r="4"/>
|
<circle cx="12" cy="7" r="4"/>
|
||||||
@@ -121,15 +125,13 @@ $agents_query = new WP_Query([
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</article>
|
</article>
|
||||||
<?php endwhile; ?>
|
<?php endforeach; ?>
|
||||||
</div>
|
</div>
|
||||||
<?php else : ?>
|
<?php else : ?>
|
||||||
<div class="no-agents-message">
|
<div class="no-agents-message">
|
||||||
<p>No team members to display at this time.</p>
|
<p>No team members to display at this time.</p>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php wp_reset_postdata(); ?>
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user