Skip to content

Commit

Permalink
Filter searches by allowed post types if post_type is not set (#2816)
Browse files Browse the repository at this point in the history
Fixes #2815
  • Loading branch information
adamwoodnz authored Aug 2, 2024
1 parent 6f3ee52 commit 39a7719
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion wp-content/themes/pub/wporg-learn-2024/inc/block-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function get_content_type_options( $options ) {
'lesson' => __( 'Lesson', 'wporg-learn' ),
);

$selected_slug = $wp_query->get( 'post_type' ) ? $wp_query->get( 'post_type' ) : 'any';
$post_type = $wp_query->get( 'post_type' );
$selected_slug = is_string( $post_type ) ? $post_type : 'any';
$label = $options[ $selected_slug ] ?? $options['any'];

return array(
Expand Down
18 changes: 18 additions & 0 deletions wp-content/themes/pub/wporg-learn-2024/inc/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

add_action( 'pre_get_posts', __NAMESPACE__ . '\add_language_to_archive_queries' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\add_excluded_to_lesson_archive_query' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\filter_search_queries_by_post_type' );
add_filter( 'request', __NAMESPACE__ . '\handle_all_level_query' );

/**
Expand Down Expand Up @@ -80,6 +81,23 @@ function add_excluded_to_lesson_archive_query( $query ) {
}
}

/**
* Filter search queries by post type.
* Only include courses and lessons in search results unless post_type is set, eg. for an archive search.
*
* @param WP_Query $query The query object.
* @return WP_Query The modified query object.
*/
function filter_search_queries_by_post_type( $query ) {
if ( ! is_admin() && $query->is_search() && $query->is_main_query() ) {
if ( ! $query->get( 'post_type' ) ) {
$query->set( 'post_type', array( 'course', 'lesson' ) );
}
}

return $query;
}

/**
* Modify the request.
*
Expand Down

0 comments on commit 39a7719

Please sign in to comment.