WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Loop] Multiple loops on a category template (10 posts)

  1. yekxmerr
    Member
    Posted 1 year ago #

    Hello guys.
    I'm having some problems on multiple looping in my category template file.
    I'm using this to list the popular posts found in the current category:

    <?php $popular_loop_category = new WP_Query( array('v_sortby' => 'views', 'v_orderby' => 'desc', 'posts_per_page' => '4', 'category__in' => wp_get_post_categories( $post->ID )));
        if( $popular_loop_category->have_posts() ): while( $popular_loop_category->have_posts() ): $popular_loop_category->the_post(); $do_not_duplicate[] = $post->ID ?>

    and on the main loop i would like to exclude the posts from the previous wp-query:

    <?php query_posts(array('post__not_in'=>$do_not_duplicate));
    	if (have_posts()) : while (have_posts()) : the_post(); ?>

    But this isn't working because i'm doing something wrong. Any suggestions?

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

  3. yekxmerr
    Member
    Posted 1 year ago #

    Sorry for not being very specific. My problem is that i want to exclude posts on the main loop, and this posts are in a wp_query.
    Example:
    In my category template i show the top 5 popular posts across the pages, and i want to avoid duplicated content on the main loop.

  4. Jay Hoffmann
    Member
    Posted 1 year ago #

    The best way to do this is through pre_get_posts. Altering the main query through query_posts can get messy quick. What pre_get_posts allows you to do is alter the main query before it is even run, so you can exclude those category posts no matter where the main query is called.

    The is_main_query function is used to hook into just the primary query when you use pre_get_posts, so that you will not affect any other queries that you have set up.

    In your functions.php (or even better, maybe through a plugin?) you will run something like this. Just change the category number to your category ID, and leave the negative sign in there to signal that you would like to exclude this category.

    add_action( 'pre_get_posts', 'exclude_popular_posts );
    function exclude_popular_posts( $query ) {
        if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
            $query->set( 'cat', '-5' );
    }
  5. yekxmerr
    Member
    Posted 1 year ago #

    Ok i'm using now the pre_get_posts and wow it's awesome. But this still doesn't solve my problem.
    $query->set( 'cat', '-5' );
    I would need it to use the current category but i don't know how to achieve this. I think i need to call $args but i'm not shure.

    And i have another problem, i have this wp_query

    <?php $popular_loop = new WP_Query( array('v_sortby' => 'views', 'v_orderby' => 'desc', 'posts_per_page' => '4' ));
        if( $popular_loop->have_posts() ): while( $popular_loop->have_posts() ): $popular_loop->the_post(); ?>

    But this code will call out all the posts and i need to get the popular ones on the category i'm currently on.
    Sorry for asking this questions, these are probably WordPress dummy stuff

  6. yekxmerr
    Member
    Posted 1 year ago #

    Update: The problem i'm facing now is that i need to create a WP_query that gets the top 4 most viewed post (this i know how) and in the category of the main loop (this i don't).
    So i need to extract the category id or something from the main loop into a variable and then get it in the wp_query? Or there is a simple way to achieve this?

    Update2:
    I used this on the main loop:
    <?php global $post; if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
    And this on the wp_query:

    <?php $popular_loop = new WP_Query(array('v_sortby' => 'views', 'v_orderby' => 'desc', 'posts_per_page' => '4', 'cat' => $post>category_id));
        if( $popular_loop->have_posts() ): while( $popular_loop->have_posts() ): $popular_loop->the_post(); $do_not_duplicate = $post->ID; ?>
    But right now i get the data backwards but i think i can fix it. Are the queries ok?

  7. supportie
    Member
    Posted 1 year ago #

    Not sure I am on the right track, but you could get the current category id on the archive page using something like:

    $category = get_queried_object();
    $popular_loop = new WP_Query(array('v_sortby' => 'views', 'v_orderby' => 'desc', 'posts_per_page' => '4', 'cat' => $category->term_id));
  8. yekxmerr
    Member
    Posted 1 year ago #

    I still can't get there. Damn this isn't easy but i won't rest until i get this to work. The problem is my lack of skills in php :(
    I still can't get the category id from the main loop, and avoid the duplicate issues on the wp_query.
    Simple Mode On:

    - Query1( Wp_query that lists the 4 most popular posts of the main loop category, avoiding the duplicate on the main loop )
    &
    - Query2 ( Main Loop that lists the posts from the current category and i have a pre_get_posts hook that forces it to list 36 posts per page and it needs to avoid duplicated content from the wp_query )

  9. yekxmerr
    Member
    Posted 1 year ago #

    I'm still working on this and i can't get it done.
    It is a category.php theme file and i have a normal loop to display the category posts, and now i need a wp_query to show the popular posts of the same category, but this wp_query is displayed first (don't know if this matters).
    Getting killed on this lol

  10. yekxmerr
    Member
    Posted 1 year ago #

    Ok after some hours i got this working:
    Wp_query:

    <?php  $cat_ID = get_query_var('cat'); $popular_loop = new WP_Query('cat='.$cat_ID.'&showposts=3&v_sortby=views');
        if( $popular_loop->have_posts() ): while( $popular_loop->have_posts() ): $popular_loop->the_post(); $do_not_duplicate[] = $post->ID; ?>

    Main Loop:
    <?php if (have_posts()) : while (have_posts()) : the_post(); if(in_array($post->ID, $do_not_duplicate)) continue; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic