这就是我如何覆盖 WordPress 搜索页面以实现更好的结果,这要归功于 Relevanssi:
global $wp_query;
$search_txt = get_query_var('s');
$args = array(
's' => $search_txt,
'post_type' => 'formations',
'posts_per_page' => 9,
'paged' => $wp_query->query_vars['paged'], // conserver le numéro de page de la requête initiale
);
// filtrer suivant la bonne taxonomy
if (isset($_GET['taxonomy'])) {
switch ($_GET['taxonomy']) {
case 'formation-diplomantes-cpf':
$ta = ['formation-diplomante', 'formation-eligible-au-cpf'];
$op = 'AND';
break;
case 'toute-formation':
break;
default:
$ta = $_GET['taxonomy'];
$op = 'IN';
}
if (isset($ta)) {
$tq = [[
'taxonomy' => 'type_form',
'field' => 'slug',
'terms' => $ta,
'operator' => $op,
]];// Tax Query
$args['tax_query'] = $tq;
}
}
$fq = new WP_Query();
$fq->parse_query( $args );
relevanssi_do_query($fq);
$any_formation = false;
$fdia = [];// Formations DIOGEN IDs Array
$fia = [];// Formations IDs Array
$i=0;
while ($fq->have_posts()) : $fq->the_post();
if ( 'formations' == get_post_type() ) {
$i++;
$fdia[get_the_ID()] = get_field('id_diogen', get_the_ID());
$fia[] = get_the_ID();
$any_formation = true;
}
endwhile;
?>
结果是分页的:
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $fq->max_num_pages
) );
这在大多数情况下都很有效:
- https://cdma.happy-dev.fr/?taxonomy=toute-formation&s=design
- https://cdma.happy-dev.fr/page/2/?taxonomy=toute-formation&s=design
- https://cdma.happy-dev.fr/page/3/?taxonomy=toute-formation&s=design
它在其他人中失败:
- https://cdma.happy-dev.fr/?taxonomy=toute-formation&s=sophie
- https://cdma.happy-dev.fr/page/2/?taxonomy=toute-formation&s=sophie
- https://cdma.happy-dev.fr/page/3/?taxonomy=toute-formation&s=sophie
我最终明白,当我没有足够的实际值时,我的编队分页不起作用。也就是说,假设我有一个带有 10 个编队和 20 个实际的查询:一切都按预期工作。然而,相反的失败。
这就是我重写有关实际情况的 URL 的方式:
function custom_rewrite_rules( $wp_rewrite ) {
$wp_rewrite->rules = array(
'actualite/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?pagename=actualite&paged=' . $wp_rewrite->preg_index( 1 ),
) + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'custom_rewrite_rules' );
我试图在搜索 URL 上加上一个前缀,以为可以发挥作用,但无济于事。我还尝试为搜索页面添加自定义重写规则,但没有任何影响。我仍然得到404。我花了几个小时挖掘每一个可能的方向,但无济于事。
任何建议都是最受欢迎的。我不明白为什么形式的分页和实际的分页都是相关的。