1

Im using pre_get_posts for my taxonomy.php for my products and paginations to work properly. And I have my pre_get_posts in my functions.php. This is how that current code looks in my functions. ( Provided by @PieterGoosen )

add_action( 'pre_get_posts', function ( $q ) {
    if( !is_admin() && $q->is_main_query() && $q->is_tax() ) {
        $q->set( 'posts_per_page', 10 );
        $q->set( 'orderby', 'name' );
        $q->set( 'order', 'ASC' );
    }
});

as you can see $q->set( 'posts_per_page', 10 ); is how I set my posts per page.

and in my taxonomy.php I'm running my loop like so:

<div class="hidden-sm">

    <?php while( have_posts() ) : the_post(); ?>

    //loop content

    <?php the_field('image'); ?>

    <?php endwhile; ?>
</div><!--hidden-sm-end-->

     //paginations   

        <?php 
        global $wp_query;

        $total_pages = $wp_query->max_num_pages;

        if ($total_pages > 1){

          $current_page = max(1, get_query_var('paged'));

          echo paginate_links(array(
              'base' => get_pagenum_link(1) . '%_%',
              'format' => '/page/%#%',
              'current' => $current_page,
              'total' => $total_pages,
              'before_page_number' => '<div class="pagination-navigation">',
              'after_page_number' => '</div>'

            ));
        }
        ?>

loop content for mobile:

<div class="visible-sm">

     <?php while( have_posts() ) : the_post(); ?>

        //loop content that now shows do to media queries but now want it to be posts_per_page 6

    <?php the_field('image'); ?>

    <?php endwhile; ?>

    </div><--sm visible end-->
     //paginations   

        <?php 
        global $wp_query;

        $total_pages = $wp_query->max_num_pages;

        if ($total_pages > 1){

          $current_page = max(1, get_query_var('paged'));

          echo paginate_links(array(
              'base' => get_pagenum_link(1) . '%_%',
              'format' => '/page/%#%',
              'current' => $current_page,
              'total' => $total_pages,
              'before_page_number' => '<div class="pagination-navigation">',
              'after_page_number' => '</div>'

            ));
        }
        ?>

So I have 2 different loop contents one for mobile and one for desktop version and they both only show in a certain screen size do to media queries.

So my question is when my loop content for mobile displays how can I give that a different posts_per_page parameter if the pre_get_posts is set for everything in taxonomy in the functions.php.

Meaning that when I see everything for desktop because the screen size is lets just say over 700 px the posts per page is 10 but when I see the loop content for mobile I want the posts per page set to 6.

Any idea how I can set a $q->set( 'posts_per_page', 6 ); only for another loop in taxonomy.php ?

UPDATE

Heres the same question but in a different form that may be a little bit more understandable with my goal. Ok so in my taxonomy.php I have a regular loops running

<?php while( have_posts() ) : the_post(); ?>

Now in my functions.php I set it's parameters for pre_get_posts with

$q->set( '' );

So as you can see my pre_get_posts code above I set all the parameters for the taxonomy page. Now lets say I wanted to run another query on taxonomy page but with different parameters how can I go about doing that. For example lets say on the side of my page im loading a query of popular items or most viewed items or whatever requires different parameter, how would I go about giving different parameters to that specific query on the taxonomy page if all the parameters have already been set with pre_get_posts in the functions? I want to give unique parameters to specific queries on my taxonomy page and not generalize everything.

Would I just run another loop but with WP_Query and set $args ?

  • In my honest opinion, I would really drop this and go with either 6 or 10 posts for all devices – Pieter Goosen Dec 15 '14 at 17:40
  • 1
    there's no 100% reliable way to do this. the best you will ever get is it will work for some people. – Milo Dec 15 '14 at 17:42
  • @PieterGoosen I ask because it's not just posts_per_page but it can be many other parameter like order_by and other parameters. I don't want to be limited to only using one parameter for everything in one page that loads different queries. So I can probably just use 10 for all but what if I wanted to specify another parameter for another query on the same page which is very likely to happen. Thats why I want to know how to give a unique parameter on the same page but to a different query – Lucas Santos Dec 15 '14 at 17:49
  • Updated my question at the bottom. – Lucas Santos Dec 15 '14 at 18:16
  • It is unfortunately as @Milo said, it will only work for some. PHP was never intented to run client/browser side queries. Relying on client side stuff to make something work in php is like swimming with sharks and hoping they won't bite you. – Pieter Goosen Dec 15 '14 at 18:19

Your Answer

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

Browse other questions tagged or ask your own question.