0

Googled for hours and couldn't find solution anywhere. =)

This solution doesn't work as well: Display all posts in current category

Regardless of how I alter my query_posts - it always displays all posts in all categories.

$cat = get_query_var('cat');
$PozCat = get_category ($cat);
//$PozCat->id
query_posts('posts_per_page=-1&cat='.$PozCat->id);

    while ( have_posts() ) : the_post();

        /* Include the post format-specific template for the content. If you want to
         * this in a child theme then include a file called called content-___.php
         * (where ___ is the post format) and that will be used instead.
         */
        get_template_part( 'content', 'category' );

    endwhile;
0

query_posts breaks the main query object ($wp_query) which holds the query object and all relevant query info which breaks all conditional tags and pagination and page functionality, which simply means that it is totally useless trying to use any info regarding the main query object as it was all corrupted by query_posts resetting the main query.

That is the number one reason one should never ever use query_posts.

As this is a category page, simply remove the following lines

cat = get_query_var('cat');
$PozCat = get_category ($cat);
//$PozCat->id
query_posts('posts_per_page=-1&cat='.$PozCat->id);

Your category page will work normal again. If you need to alter the main query on your category page, use pre_get_posts to correctly alter the query variables before the SQL is build to run the main query. The following will return all posts on you category pages

add_action( 'pre_get_posts', function ( $q )
{
    if (    !is_admin() // Only target front end
         && $q->is_main_query() // Only target the main query
         && $q->is_category() // Only target category pages, change to $q->is_tax() for taxonomy pages
    ) {
        $q->set( 'posts_per_page', -1 );
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.