Skip to content

Commit 9005657

Browse files
wpscholarclaude
andcommitted
Fix custom taxonomy query structure in build_query_args method
The tax_query argument for WP_Query requires an array of arrays, not a flat array. Added unit test to verify proper structure for custom taxonomy queries. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent ed7cdc4 commit 9005657

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

RandomPostOnRefresh.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,11 @@ public static function build_query_args( $atts ) {
280280
$query_args['tag__in'] = $terms;
281281
} else {
282282
$query_args['tax_query'] = array(
283-
'taxonomy' => $atts['taxonomy'],
284-
'terms' => $terms,
283+
array(
284+
'taxonomy' => $atts['taxonomy'],
285+
'field' => 'term_id',
286+
'terms' => $terms,
287+
),
285288
);
286289
}
287290
}
@@ -371,4 +374,4 @@ public static function error( $message, $example = '' ) {
371374

372375
add_action( 'plugins_loaded', array( 'RandomPostOnRefresh', 'initialize' ) );
373376

374-
}
377+
}

tests/RandomPostOnRefreshTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,27 @@ public function test_build_query_args_with_taxonomy() {
134134
$args = RandomPostOnRefresh::build_query_args( $atts );
135135
$this->assertEquals( array( 3, 4 ), $args['category__in'] );
136136
}
137+
138+
/**
139+
* Test build_query_args with custom taxonomy and terms.
140+
*/
141+
public function test_build_query_args_with_custom_taxonomy() {
142+
$atts = array_merge(
143+
RandomPostOnRefresh::DEFAULT_ATTRIBUTES,
144+
array(
145+
'post_type' => 'post',
146+
'taxonomy' => 'custom_tax',
147+
'terms' => '5,6',
148+
)
149+
);
150+
$args = RandomPostOnRefresh::build_query_args( $atts );
151+
$this->assertArrayHasKey( 'tax_query', $args );
152+
$this->assertIsArray( $args['tax_query'] );
153+
$this->assertIsArray( $args['tax_query'][0] );
154+
$this->assertEquals( 'custom_tax', $args['tax_query'][0]['taxonomy'] );
155+
$this->assertEquals( 'term_id', $args['tax_query'][0]['field'] );
156+
$this->assertEquals( array( 5, 6 ), $args['tax_query'][0]['terms'] );
157+
}
137158

138159
/**
139160
* Test build_query_args with show=image and image_required=true.

0 commit comments

Comments
 (0)