Поэтому, если я ищу партнера, он фильтрует результаты по связанным проектам.
Что я пробовал:
< ul>
[*]Настройки запроса Elementor: попытка фильтрации по Поля ACF, но не удалось найти ни одной встроенной функции.
[*]Пользовательский запрос: пытался изменить поисковые запросы с помощью PHP, но возникли проблемы с экранированием значений.
Где я оказался:
Код: Выделить всё
function custom_elementor_search_by_partner_title( $query ) {
// Check if the search is targeting a specific post type (e.g., 'project')
if ( isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] === 'project' ) {
// Get the search term entered by the user
$search_term = get_query_var( 's' );
if ( ! empty( $search_term ) ) {
// Get all Partner posts that match the search term
$partners = get_posts([
'post_type' => 'partner',
// 'partner' is the post type key
's' => $search_term,
// Searching by partner title
'fields' => 'ids',
// Return only IDs
'posts_per_page' => -1,
// Get all matching partners
]);
// If matching partners are found, add them to the meta query
if ( ! empty( $partners ) ) {
$partner_ids = $partners;
// Array of matching Partner post IDs
// Ensure we're passing the partner IDs correctly as an array in meta_query
$meta_query = array(
array(
'key' => 'partner',
// ACF relationship field name
'value' => $partner_ids,
// Array of partner IDs
'compare' => 'IN',
// Compare against the partner post IDs
),
);
// Set the meta query to filter by related partners
$query->set( 'meta_query', $meta_query );
}
}
}
}
add_action( 'pre_get_posts', 'custom_elementor_search_by_partner_title' );
Код: Выделить всё
function custom_search_query( $query ) {
// Check if the search is targeting a specific post type (e.g., 'project')
if ( isset( $query->query_vars['post_type'] ) && $query->query_vars['post_type'] === 'project' ) {
// Get the search term entered by the user
$search_term = get_query_var( 's' );
if ( ! empty( $search_term ) ) {
// Get all Partner posts that match the search term
$partners = get_posts([
'post_type' => 'partner',
// 'partner' is the post type key
's' => $search_term,
// Searching by partner title
'fields' => 'ids',
// Return only IDs
'posts_per_page' => -1,
// Get all matching partners
]);
// If matching partners are found, add them to the meta query
if ( ! empty( $partners ) ) {
$partner_ids = $partners;
// Array of matching Partner post IDs
// Ensure we're passing the partner IDs correctly as an array in meta_query
$meta_query = array(
array(
'key' => 'partner',
// ACF relationship field name
'value' => $partner_ids,
// Array of partner IDs
'compare' => 'IN',
// Compare against the partner post IDs
),
);
// Set the meta query to filter by related partners
$query->set( 'meta_query', $meta_query );
}
}
}
}
add_action('elementor/query/custom_search', 'custom_search_query');
- Кто-нибудь успешно фильтровал по полю ACF CPT в Elementor?< /li>
Следует ли мне перейти на платный плагин, такой как JetEngine или SearchWP, или есть лучший способ добиться этого в Elementor?
Подробнее здесь: https://stackoverflow.com/questions/792 ... field-meta