0

I'm trying to list advertisers in 3 categories (custom taxonomy "ad_category") as basic, casual and premium. As default query one page contains 10 results but it should be in order to display premium advertisers at top then casual and basic advertisers at last.

Ex: If I'm having a total of 30 premium and casual ads, the basic ads starts from page 4.

My Codes are:

loop-suppliers-basic.php

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$title = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy') );
$args = array(
    'post_type' => 'suppliers',
    'tax_query' => array(
        array(
            'taxonomy' => 'ad_category',
            'field'    => 'slug',
            'terms'    => array('basic_advertiser'),
        ),
        array(
            'taxonomy' => 'supplier_categories',
            'field'    => 'slug',
            'terms'    => $title->slug,
        ),
    ),
    'paged' => $paged,
    'orderby' => 'rand'
);

$the_query = new WP_Query( $args ); ?>

<?php if ($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>

////////// Loop goes here /////////

<?php endwhile; endif; ?>

taxonomy-supplier_categories.php

<?php get_template_part('loop-suppliers-premium'); ?>
<?php get_template_part('loop-suppliers-casual'); ?>
<?php get_template_part('loop-suppliers-basic'); ?>
<?php get_template_part('pagination'); ?>

Pagination function

function wp_pagination() { global $wp_query; $big = 999999999; echo paginate_links(array( 'base' => str_replace($big, '%#%', get_pagenum_link($big)), 'format' => '?paged=%#%', 'current' => max(1, get_query_var('paged')), 'total' => $wp_query->max_num_pages )); }

This does display in order I want but the pagination goes wrong. Tried using methods like **rewind_posts();*but didn't work.

7
  • what is 'wrong'? what code are you using for the pagination?
    – Michael
    Commented Apr 19, 2018 at 2:14
  • Hi Michael, it's the default pagination I'm using. I'll add the pagination code to the question as well. What happens is if I have 2 premium ads, 2 casual ads and 10 basic ads it all displays in the first page. Instead it should display the 2 premium ads at top 2 casual ads next and first 6 basic ads then paged to No:2
    – JudeAinsly
    Commented Apr 19, 2018 at 2:23
  • what is your default posts_per_page value? you have 3 separate queries, you will have a number of results up to 3 times whatever posts_per_page is. also note that you can't randomize order this way with pagination, you will get a random selection on each page with no relationship to the random selection on all the other pages, meaning you will have repeats.
    – Milo
    Commented Apr 19, 2018 at 4:20
  • Hi Milo, default posts_per_page value is 10. Yes, I get your point. Ignore the orderby. Is there any method to achieve this without having 3 separate queries in 3 files?
    – JudeAinsly
    Commented Apr 19, 2018 at 4:43
  • Did you try adding posts_per_page to the $args parameter of each of the 3 separate queries? E.g. if you want 6 posts/ads displayed per page, you'd add 'posts_per_page' => 6 to the $args
    – Sally CJ
    Commented Apr 19, 2018 at 7:20

1 Answer 1

0

try now this code

    <?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$title = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy') );
$args = array(
    'post_type' => 'suppliers',
    'tax_query' => array(
        array(
            'taxonomy' => 'ad_category',
            'field'    => 'slug',
            'terms'    => array('basic_advertiser'),
        ),
        array(
            'taxonomy' => 'supplier_categories',
            'field'    => 'slug',
            'terms'    => $title->slug,
        ),
    ),
    'paged' => $paged,
    'orderby' => 'rand'
);

$the_query = new WP_Query( $args ); ?>

<?php if ($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>

////////// Loop goes here /////////

<?php endwhile; ?>

<nav class="pagination">
        <?php pagination_bar( $the_query ); ?>
</nav>
<?php wp_reset_postdata();

endif;



function wp_pagination($wp_query)
{
    $big = 999999999;
    echo paginate_links(array(
        'base' => str_replace($big, '%#%', get_pagenum_link($big)),
        'format' => '?paged=%#%',
        'current' => max(1, get_query_var('paged')),
        'total' => $wp_query->max_num_pages
    ));
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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