我对WP开发相当陌生,但据我所知,我在我的自定义归档页面(下面)中遵循了正确的过程。如果我删除is_main_query()
它会清除菜单,并且可能会由于应用过滤器而丢失许多其他数据。
添加调试代码表明is_main_query()
是否从未返回为true?我做错了什么?
我的版本是WP 4.4,带有“Avada”主题,我还利用ACF高级自定义字段将自定义元数据添加到我的帖子中。
非常感谢您的帮助,谢谢!
<?php
$has_searchform = true;
get_header();
?>
<div id="content" <?php Avada()->layout->add_class( \'content_class\' ); ?> <?php Avada()->layout->add_style( \'content_style\' ); ?>>
<?php
function my_pre_get_posts( $query ) {
//* if within admin, return
if ( is_admin() ) {
echo "Admin returning";
return;
}
//* if not main query, return
else if ( ! $query->is_main_query() ) {
echo "Not main query";
return;
}
else if ( $query->is_main_query() ) {
echo "Main query called";
if( (isset($_GET[\'fs_date_from\']) && $_GET[\'fs_date_from\'] != \'\') ||
(isset($_GET[\'fs_date_to\']) && $_GET[\'fs_date_to\'] != \'\') ||
(isset($_GET[\'fs_music_genre\']) && $_GET[\'fs_music_genre\'] != \'\') ||
(isset($_GET[\'fs_keyword\']) && $_GET[\'fs_keyword\'] != \'\') ||
(isset($_GET[\'fs_country\']) && $_GET[\'fs_country\'] != \'\') ||
(isset($_GET[\'fs_festival_category\']) && $_GET[\'fs_festival_category\'] != \'\')) {
if(isset($_GET[\'fs_date_from\']) && $_GET[\'fs_date_from\'] != \'\') {
$date_from = date(\'Ymd\',strtotime(sanitize_text_field($_GET[\'fs_date_from\'])));
$search_array[\'fs_date_from\'] = array( \'key\' => \'start_date\',
\'value\' => $date_from,
\'compare\' => \'>=\'
);
}
if(isset($_GET[\'fs_date_to\']) && $_GET[\'fs_date_to\'] != \'\') {
$date_to = date(\'Ymd\',strtotime(sanitize_text_field($_GET[\'fs_date_to\'])));
$search_array[\'fs_date_to\'] = array(\'key\' => \'end_date\',
\'value\' => $date_to,
\'compare\' => \'<=\');
}
$keyword = sanitize_text_field($_GET[\'fs_keyword\']);
if($keyword != \'\') {
$query->set(\'s\',$keyword);
}
if(isset($_GET[\'fs_country\']) && $_GET[\'fs_country\'] != \'\') {
$country = sanitize_text_field($_GET[\'fs_country\']);
$search_array[\'fs_country\'] = array(\'key\' => \'fs_country\',
\'value\' => $country,
\'compare\' => \'=\');
}
$music_genre = sanitize_text_field($_GET[\'fs_music_genre\']);
if($music_genre != \'\') {
$tax_query[\'music_genre\'] = array( \'taxonomy\' => \'music_genre\',
\'field\' => \'id\',
\'terms\' => array($music_genre),
\'operator\' => \'IN\');
}
$festival_category = sanitize_text_field($_GET[\'fs_festival_category\']);
if($festival_category != \'\') {
$tax_query[\'fs_festival_category\'] = array( \'taxonomy\' => \'festival_category\',
\'field\' => \'id\',
\'terms\' => array($festival_category),
\'operator\' => \'IN\');
}
print_r($search_array);
$search_array[\'relation\'] = (($date_from != \'\' && $date_to != \'\')? "AND" : "");
$query->set(\'posts_per_page\', 10);
$query->set(\'post_type\' , \'festival\');
$query->set(\'meta_query\' , $search_array);
$query->set(\'tax_query\' , $tax_query);
}
else {
echo "Woop";
$query->set(\'posts_per_page\', 10);
$query->set(\'post_type\' , \'festival\');
}
}
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\');
if( have_posts() ) {
echo
"<div class=\'row fusion-row\'>
<div class=\'col-sm-12\'>
";
fusion_pagination($pages = \'\', $range = 2);
echo
" </div>
</div>";
while(have_posts()) {
the_post();
//setup_postdata( $post );
$image = get_field(\'header_image\');
$image_url = $image[\'sizes\'][\'medium_large\'];
$date_format = \'dS \\o\\f F Y\';
/*if(get_field(\'start_date\') != \'\') {
$start_date = DateTime::createFromFormat(\'Ymd\', get_field(\'start_date\'));
}
else {
$start_date = \'\';
}
if(get_field(\'end_date\') != \'\') {
$end_date = DateTime::createFromFormat(\'Ymd\', get_field(\'end_date\'));
}
else {
$end_date = \'\';
}*/
$start_date = get_field(\'start_date\');
$end_date = get_field(\'end_date\');
$vibe = substr(get_field(\'vibe\'), 0,250)."...";
$location = get_field(\'location\');
$country = get_field(\'country\');
//print_r($post);
$price = get_min_price(get_the_ID());
echo
"<div class=\'row fusion-row result_row\' onclick=\\"javascript:window.location.href=\'". get_permalink() ."\'; return false;\\">
<div class=\'col-sm-5 result_thumbnail\' style=\'background: url(\\"". $image_url ."\\") center center no-repeat;\'>
</div>
<div class=\'col-sm-7\'>
<h3 class=\'result_title\'><a href=\'". get_permalink() ."\'>".get_the_title()."</a></h3>
<h4 class=\'result_location\'>$location" . (($location != \'\' && $country != \'\')? "," : "") . " $country</h4>
<div class=\'result_vibe\'>
". $vibe ."
</div>
</div>
<div class=\'result_bottom_row col-sm-7\'>
<div class=\'result_dates\'>
";
if($start_date != \'\') {
echo $start_date;
}
echo ((get_field(\'start_date\') != \'\' && get_field(\'end_date\') != \'\')? " - " : "");
if($end_date != \'\') {
echo $end_date;
}
echo
" </div>
<div class=\'result_price\'>
$price
</div>
</div>
</div>
";
}
fusion_pagination($pages = \'\', $range = 2);
}
else {
echo
"<p>Sorry, no Festivals match your search.</p>";
}
?>
</div>
<?php do_action( \'fusion_after_content\' ); ?>
<?php get_footer();
// Omit closing PHP tag to avoid "Headers already sent" issues.