Remove meta query from search

This was too expensive. Hiding results is not great but at least feasible.
This commit is contained in:
Tetrakern 2023-11-12 15:58:02 +01:00
parent 635b73abc8
commit d3738ec3e3
4 changed files with 60 additions and 46 deletions

View File

@ -240,7 +240,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `login_head` | `fictioneer_wp_login_scripts`
| `manage_comments_custom_column` | `fictioneer_add_comments_report_column_content`
| `personal_options_update` | `fictioneer_update_admin_user_profile`, `fictioneer_update_my_user_profile`
| `pre_get_posts` | `fictioneer_extend_search_query`, `fictioneer_remove_unlisted_from_search`, `fictioneer_read_others_files`, `fictioneer_read_others_files_list_view`, `fictioneer_filter_chapters_by_story`
| `pre_get_posts` | `fictioneer_extend_search_query`, `fictioneer_read_others_files`, `fictioneer_read_others_files_list_view`, `fictioneer_filter_chapters_by_story`
| `private_to_draft` | `fictioneer_chapter_to_draft`
| `publish_to_draft` | `fictioneer_chapter_to_draft`
| `rest_api_init` | `fictioneer_register_endpoint_get_story_comments`

View File

@ -1511,10 +1511,21 @@ if ( ! function_exists( 'fictioneer_echo_card' ) ) {
// Echo correct card by post type
switch ( $type ) {
case 'fcn_chapter':
get_template_part( 'partials/_card-chapter', null, $args );
if (
fictioneer_get_field( 'fictioneer_chapter_hidden', get_the_ID() ) ||
fictioneer_get_field( 'fictioneer_chapter_no_chapter', get_the_ID() )
) {
get_template_part( 'partials/_card-hidden', null, $args );
} else {
get_template_part( 'partials/_card-chapter', null, $args );
}
break;
case 'fcn_story':
get_template_part( 'partials/_card-story', null, $args );
if ( fictioneer_get_field( 'fictioneer_story_hidden', get_the_ID() ) ) {
get_template_part( 'partials/_card-hidden', null, $args );
} else {
get_template_part( 'partials/_card-story', null, $args );
}
break;
case 'fcn_recommendation':
get_template_part( 'partials/_card-recommendation', null, $args );

View File

@ -176,49 +176,6 @@ if ( ! function_exists( 'fcn_keyword_search_authors_input' ) ) {
// FILTER SEARCH QUERY
// =============================================================================
/**
* Remove unlisted chapters and stories from search results
*
* @since 5.2.4
*
* @param WP_Query $query The query.
*/
function fictioneer_remove_unlisted_from_search( $query ) {
// Only for search queries on the frontend...
if ( ! is_admin() && $query->is_main_query() && $query->is_search ) {
$query->set(
'meta_query',
array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_chapter_hidden',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'fictioneer_chapter_hidden',
'value' => '0'
)
),
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
)
)
);
}
}
add_action( 'pre_get_posts', 'fictioneer_remove_unlisted_from_search', 10 );
/**
* Extend search query with custom input
*

View File

@ -696,5 +696,51 @@ function fictioneer_delete_null_option( $option, $old_value, $value ) {
}
add_action( 'updated_option', 'fictioneer_delete_null_option', 20, 3 );
// =============================================================================
// FILTER SEARCH QUERY
// =============================================================================
/**
* Remove unlisted chapters and stories from search results
*
* @since 5.2.4
*
* @param WP_Query $query The query.
*/
function fictioneer_remove_unlisted_from_search( $query ) {
// Only for search queries on the frontend...
if ( ! is_admin() && $query->is_main_query() && $query->is_search ) {
$query->set(
'meta_query',
array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_chapter_hidden',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'fictioneer_chapter_hidden',
'value' => '0'
)
),
array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
)
)
);
}
}
add_action( 'pre_get_posts', 'fictioneer_remove_unlisted_from_search', 10 );
?>