Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

php - Notice: is_main_query was called incorrectly

Was searching for two days what makes problem in my WordPress theme, and using debug I found this error, and that what I have in functions about that error code.

It displays notice on place where section box should be shown.

How can I fix this?

Notice: is_main_query was called incorrectly. In pre_get_posts, use the WP_Query->is_main_query() method,


// Filter to "pre_get_posts" to change query vars
add_action( 'pre_get_posts', 'dp_custom_get_posts' );
function dp_custom_get_posts( $query ) {
    if(is_admin())
        return;

    $orderby = $query->get('orderby');
    $order = $query->get('order');

    // If no 'orderby' specified, get first sort type from selected sort types
    $selected_sort_types = dp_selected_sort_types();
    if(is_main_query() && !empty($selected_sort_types) && empty($orderby)) {
        $_sort_types = array_keys($selected_sort_types);
        $orderby = $_sort_types[0];
        $query->set('orderby', $orderby);
    }

    // Reset query vars based orderby parameter
    if($orderby == 'comments') {
        $query->set('orderby', 'comment_count');
    } 
    elseif($orderby == 'views') {   
        $query->set('orderby', 'meta_value_num');
        $query->set('meta_key', 'views');

        // The arguments for BAW Post Views Count plugin
        if(function_exists('baw_pvc_main')) {
            global $timings;
            $views_timing = $query->get('views_timing') ? $query->get('views_timing') : 'all';
            $date = $views_timing == 'all' ? '' : '-'. date( $timings[$views_timing] );
            $meta_key = apply_filters( 'baw_count_views_meta_key', '_count-views_' . $views_timing . $date, $views_timing, $date );
            $query->set('meta_key', $meta_key);
        }
    } 
    elseif($orderby == 'likes') {   
        $query->set('orderby', 'meta_value_num');
        $query->set('meta_key', 'likes');
    } 
    elseif($orderby == 'title' && !$order) {
        // If order by title, and no order specified, set "ASC" as default order.
        $query->set('order', 'ASC');
    }

    // Only display posts on search results page
    if (is_search() && $query->is_main_query())
        $query->set('post_type', 'post');

    // Make tax_query support "post-format-standard"
    $tax_query = $query->get('tax_query');

    if(!empty($tax_query)) {
        foreach($tax_query as $index => $single_tax_query) {
            if(empty($single_tax_query['terms']))
                continue;

            $in_post_formats = (array)$single_tax_query['terms'];

            if($single_tax_query['taxonomy'] == 'post_format'
            && $single_tax_query['field'] == 'slug'
            && in_array('post-format-standard', $in_post_formats)) {
                // Get reverse operator
                $reverse_operator = 'IN';
                if(empty($single_tax_query['operator']) || $single_tax_query['operator'] == 'IN')
                    $reverse_operator = 'NOT IN';
                elseif($single_tax_query['operator'] == 'AND')
                    break;

                // Get "not in post formats"
                $post_formats = get_theme_support('post-formats');
                $all_post_formats = array();
                if(is_array( $post_formats[0])) {
                    $all_post_formats = array();
                    foreach($post_formats[0] as $post_format)
                        $all_post_formats[] = 'post-format-'.$post_format;
                }
                $not_in_post_formats = array_diff($all_post_formats, $in_post_formats);

                // Reset post_format in tax_query
                $query->query_vars['tax_query'][$index] = array(
                    'taxonomy' => 'post_format',
                    'field' => 'slug',
                    'terms' => $not_in_post_formats,
                    'operator' => $reverse_operator
                );
            }
        }
    }

    return $query;
}

This is the code of an section box, or at least part of it, I'm not sure what I'm doing anymore:

function dp_section_box($args = array()) {
    $defaults = array(
        'post_type' => 'post',
        'cat' => '',
        'taxonomies' => array(),
        'view' => 'grid-small',
        'title' => '',
        'link' => '',
        'post__in' => '',
        'posts_per_page' => '',
        'hide_if_empty' => false
    );
    $args = wp_parse_args($args, $defaults);
    extract($args);

    $posts_per_page = absint($posts_per_page);
    // Set default posts number if no specified
    if(empty($posts_per_page)) {
        if($view == 'grid-mini')
            $posts_per_page = 8;
        elseif($view == 'grid-small')
            $posts_per_page = 6;
        elseif($view == 'grid-medium')
            $posts_per_page = 4;
        elseif($view == 'list-small')
            $posts_per_page = 3;
        elseif($view == 'list-medium')
            $posts_per_page = 2;
        elseif($view == 'list-large')
            $posts_per_page = 1;
    }
    $args['posts_per_page'] = $posts_per_page;

    $args = dp_parse_query_args($args);
    $query = new WP_Query($args);

    // Output nothing if there is no posts
    if(!$query->have_posts() && $hide_if_empty)
        return;

    // Output content before section
    if(!empty($before))
        echo '<div class="section-box section-before rich-content">'. do_shortcode(wp_kses_stripslashes($before)).'</div><!-- end .section-box -->';

    // Section box begin
    echo '<div class="section-box">';

    global $section_view;   
    $section_view = $view;

    // Get term name as title
    $term = '';
    $cat = '';
    if(!empty($taxonomies['category'])) 
        $cat = $taxonomies['category'];
    if($cat)
        $term = get_term($cat, 'category');
    if(empty($title) && $term)
        $title = $term->name;
    if(empty($link) && $term)
        $link = get_term_link($term, 'category');

    $title = '<span class="name">'.$title.'</span>';

    // Add link to title and more
    $more = '';
    if($link) {
        $title = '<a class="name-link" href="'.$link.'">'.$title.'</a>';
        $more = '<a class="more-link" href="'.$link.'"><span>'.__('More', 'dp').' <i class="mini-arrow-right"></i></span></a>';
    }

    // Output section header
    echo '<div class="section-header"><h2 class="section-title">'.$title.'</h2>'.$more.'</div>';

    // Output section content
    echo '<div class="section-content '.$view.'"><div class="nag cf">';
    while ($query->have_posts()) : $query->the_post();
        get_template_part('item-video');
    endwhile;

    wp_reset_postdata();

    echo '</div></div><!-- end .section-content -->';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the solution is given in your error itself "use the WP_Query->is_main_query() method"-

try this -

if($query->is_main_query() && !empty($selected_sort_types) && empty($orderby)) {
        $_sort_types = array_keys($selected_sort_types);
        $orderby = $_sort_types[0];
        $query->set('orderby', $orderby);
    }

instead of this -

if(is_main_query() && !empty($selected_sort_types) && empty($orderby)) {
        $_sort_types = array_keys($selected_sort_types);
        $orderby = $_sort_types[0];
        $query->set('orderby', $orderby);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...