Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions includes/class-rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ public function get_collection_params() {
'type' => 'integer',
),
'post_type' => array(
'description' => __( 'Post type', 'fuxt-api' ),
'description' => __( 'Post type, or comma-separated list of post types', 'fuxt-api' ),
'type' => 'string',
'enum' => Utils::get_post_types(),
),
'fields' => array(
'description' => __( 'Additional fields to return. Comma separated string of fields.', 'fuxt-api' ),
Expand Down
53 changes: 41 additions & 12 deletions includes/utils/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,24 +342,46 @@ public static function get_posts( $params, $additional_fields ) {
}

if ( ! empty( $params['term_slug'] ) ) {
// If post_type is specified, limit taxonomy search to those registered for those post types.
if ( isset( $params['post_type'] ) ) {
$requested_types = array_map( 'trim', explode( ',', $params['post_type'] ) );
$searchable_taxonomies = array_unique( array_merge( ...array_map( 'get_object_taxonomies', $requested_types ) ) );
} else {
$searchable_taxonomies = get_taxonomies();
}

$term_slugs = array_map( 'trim', explode( ',', $params['term_slug'] ) );

$terms = get_terms(
array(
'taxonomy' => get_taxonomies(),
'slug' => $params['term_slug'],
'taxonomy' => $searchable_taxonomies,
'slug' => $term_slugs,
)
);

if ( ! empty( $terms ) ) {
$query_params['tax_query'] = array(
array(
'taxonomy' => $terms[0]->taxonomy,
// Group matched terms by taxonomy, then build one tax_query clause per taxonomy (OR relation).
$by_taxonomy = array();
foreach ( $terms as $term ) {
$by_taxonomy[ $term->taxonomy ][] = $term->slug;
}

$tax_query = array( 'relation' => 'OR' );
foreach ( $by_taxonomy as $taxonomy => $slugs ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms[0]->slug,
),
);
'terms' => $slugs,
'operator' => 'IN',
);
}
$query_params['tax_query'] = $tax_query;

$taxonomy = get_taxonomy( $terms[0]->taxonomy );
$query_params['post_type'] = $taxonomy->object_type;
// Only infer post_type from taxonomy if not explicitly provided.
if ( ! isset( $params['post_type'] ) ) {
$taxonomy = get_taxonomy( array_key_first( $by_taxonomy ) );
$query_params['post_type'] = $taxonomy->object_type;
}

// Default order is menu_order for hierarchical post types such as page.
if ( is_post_type_hierarchical( $parent_post->post_type ) ) {
Expand All @@ -373,10 +395,17 @@ public static function get_posts( $params, $additional_fields ) {

if ( ! isset( $query_params['post_type'] ) ) {
if ( isset( $params['post_type'] ) ) {
if ( ! in_array( $params['post_type'], Utils::get_post_types() ) ) {
$requested_types = array_map( 'trim', explode( ',', $params['post_type'] ) );
$valid_types = Utils::get_post_types();
$validated_types = array_filter( $requested_types, fn( $t ) => in_array( $t, $valid_types ) );

if ( empty( $validated_types ) ) {
return null;
}
$query_params['post_type'] = $params['post_type'];

$query_params['post_type'] = count( $validated_types ) === 1
? reset( $validated_types )
: array_values( $validated_types );
} else {
$query_params['post_type'] = 'post';
}
Expand Down