WordPress.org

Ready to get started?Download WordPress

Forums

Excl. cats using pre_get_posts (4 posts)

  1. RevengerTT
    Member
    Posted 1 year ago #

    I've written a quick function to exclude a category from my main query loop, however... whenever I include this it messes up the order of the loop so that things are reverse chronological - using the set 'order' doesn't seem to fix it either (see example below). Anyone else come across this and know how to resolve?

    I do not want to use the query_posts() function - in fact this gives exactly the same result.

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $idObj = get_category_by_slug('content-large-fancy-box');
            $lfb_cat_id = $idObj->term_id;
            $query->set( 'cat', '-'.$lfb_cat_id );
            $query->set( 'orderby', 'date');
            $query->set( 'order', 'DESC');
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
  2. RevengerTT
    Member
    Posted 1 year ago #

    here is a copy of the vardump($query) if it helps..

    object(WP_Query)#161 (42) { ["query_vars"]=> array(47) { ["error"]=> string(0) "" ["m"]=> int(0) ["p"]=> int(0) ["post_parent"]=> string(0) "" ["subpost"]=> string(0) "" ["subpost_id"]=> string(0) "" ["attachment"]=> string(0) "" ["attachment_id"]=> int(0) ["name"]=> string(0) "" ["static"]=> string(0) "" ["pagename"]=> string(0) "" ["page_id"]=> int(0) ["second"]=> string(0) "" ["minute"]=> string(0) "" ["hour"]=> string(0) "" ["day"]=> int(0) ["monthnum"]=> int(0) ["year"]=> int(0) ["w"]=> int(0) ["category_name"]=> string(0) "" ["tag"]=> string(0) "" ["cat"]=> string(2) "-2" ["tag_id"]=> string(0) "" ["author_name"]=> string(0) "" ["feed"]=> string(0) "" ["tb"]=> string(0) "" ["paged"]=> int(0) ["comments_popup"]=> string(0) "" ["meta_key"]=> string(0) "" ["meta_value"]=> string(0) "" ["preview"]=> string(0) "" ["s"]=> string(0) "" ["sentence"]=> string(0) "" ["fields"]=> string(0) "" ["menu_order"]=> string(0) "" ["category__in"]=> array(0) { } ["category__not_in"]=> array(0) { } ["category__and"]=> array(0) { } ["post__in"]=> array(0) { } ["post__not_in"]=> array(0) { } ["tag__in"]=> array(0) { } ["tag__not_in"]=> array(0) { } ["tag__and"]=> array(0) { } ["tag_slug__in"]=> array(0) { } ["tag_slug__and"]=> array(0) { } ["orderby"]=> string(4) "date" ["order"]=> string(4) "DESC" } ["tax_query"]=> object(WP_Tax_Query)#206 (2) { ["queries"]=> array(0) { } ["relation"]=> string(3) "AND" } ["meta_query"]=> bool(false) ["post_count"]=> int(0) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(true) ["is_404"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash"]=> string(32) "fd856b9c5c504625caed5689f119d039" ["query_vars_changed"]=> bool(false) ["thumbnails_cached"]=> bool(false) ["query"]=> array(0) { } }

  3. RevengerTT
    Member
    Posted 1 year ago #

    Have also just tried adding this to my page and stopping the pre_get_posts addition in the function.php. Gives same result, must be doing something totallllly wrong!

    $idObj = get_category_by_slug('content-large-fancy-box');
            $lfb_cat_id = $idObj->term_id;
            $args= array(
                        'orderby'=>'date',
                        'order'=>'DESC',
                        'cat'=>'-'.$lfb_cat_id,
            );
            $the_query = new WP_Query( $args );
            if ( $the_query->have_posts() ) :
    			while ( $the_query->have_posts() ) : $the_query->the_post();
  4. RevengerTT
    Member
    Posted 1 year ago #

    found this on line 2284 in query.php (wp-includes)

    changing as below resolves this issue, thanks to Peter Michael for pointing me in the right direction a long time ago! Suprised this bug still exists 4 years later!!!!!!

    if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    			//comment out the grouping and replace with no grouping
                            //$groupby = "{$wpdb->posts}.ID";
    		      $groupby = "";
    		}

    Added a tax_query() to the wp_query() as below - which looking at the code should negate the grouping... but it didn't fix. Commenting out seems the only solution - hopefully this will help someone else :)

    $args= array(
                        'orderby'=>'date',
                        'order'=>'DESC',
                        'cat'=>'-'.$lfb_cat_id,
                        'tax_query'=>array('tag__not_in' => '9999999'),
            );
            $the_query = new WP_Query( $args );

Topic Closed

This topic has been closed to new replies.

About this Topic