Domanda

In wordpress I added a function which sets the offset like so:

add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {

    if ( ! $query->is_home() ) {
        return;
    }

    $offset = 1;
    $ppp = 3;

    if ( $query->is_paged ) {
        $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
        $query->set('offset', $page_offset );
    }
    else {
        $query->set('offset',$offset);
    }
}

What is this function does is add an offset of 1 so that the first post doesn't get displayed. Which works fine perfect.

But of course with '!query->is_home' it litterly cuts every first post from every single query on my website.

What I need to do is apply this function to only one query.

So my question is: how can I select a specific query, is possible even possible do I need to do it some other way?

______________________Solution that worked for me__________________________

I was able to set the 'offset' parameter on the loop on my template page 'page-blog.php' like this without breaking the pagination(in functions.php):

add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {

        if ( !is_page_template( 'page-blog.php' )  ) {
        return ;
    }

    $offset = 1;
    $ppp = 3;

    if ( $query->is_paged() ) {
        $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
        $query->set('offset', $page_offset );
    }
    else {
        $query->set('offset',$offset);
    }
}

So now it applies the offset parameter to all loops on that page that are paged. To make it only apply to the queries that were paged I had to add the parameter 'posts_per_page'=> -1' to the query that isn't paged and doesn't need an offset. So in my case this was:

$featured_item = array(
    'post_type'=> 'post',
    'status' => 'publish',
    'posts_per_page'=> -1
);

get_posts($featured_item);

This solution allowed to display the latest post separately and of course the all the post with a pagination and offset. Maybe somebody will have use for it.

Many Thanks to s_h_dum, I am forever grateful for your help!

È stato utile?

Soluzione

If you only wish to effect the one, single query then just can pass the offset argument through the arguments array.

$offset = 1;
$ppp = 3;

if ( $query->is_paged ) {
    $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
}

blog_items = array(
    'post_type'=> 'post',
    'paged' => $paged,
    'posts_per_page'=> $ppp,
    'status' => 'publish',
    'offset' => $offset
);

$blogposts = get_posts($blog_items);

But since you are trying to paginate you may still have trouble with that. It may work better to push all of the arguments through pre_get_posts not just some of them:

// in functions.php
add_action('pre_get_posts', 'myprefix_query_offset', 1 );
function myprefix_query_offset(&$query) {

    if (is_main_query() || is_page_template( 'page-blog.php' ) ) {

      $offset = 1;
      $ppp = 3;

      if ( $query->is_paged ) {
          $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
          $query->set('offset', $page_offset );
      }
      else {
          $query->set('offset',$offset);
      }

      $query->set('post_type','post');
      $query->set('paged',$paged);
      $query->set('posts_per_page',3);
      $query->set('status','publish');

    }
}

Then a normal loop in the page template should work:

if(have_posts()) {
  while(have_posts()) {
    the_post();

  }
}

Use remove_action('pre_get_posts', 'myprefix_query_offset', 1 ); to remove the action.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top