Custom loop


Hi,

I have this work in progress : http://next.webstantly.com/blog/ , i need to have a featured post on top of the blog, so i have set up a featured cat, and the client can choose which post to have.

So i have to remove this category from the loop, i have done it like that

add_filter( 'beans_loop_query_args', 'remove_featured_from_loop' );
function remove_featured_from_loop() {
  return array(
   'cat' => '-41',
 );

}

It works, but the problem is on the pagination, if you click on page too, in reality it is page one again...



Thank you Mathieu,

It can be an idea, i have never used this function, that's why it didn't cross my mind.

But anyway i would like to know why i have this behaviour, and fix it.


Hey Alex, using sticky post as Mathieu greatly suggested is definitely a good option in your case.

Regarding the pagination issue, you have to set the paged query argument when using beans_loop_query_args because it creates a new WP_Query. So your code would be:

add_filter( 'beans_loop_query_args', 'remove_featured_from_loop' );
function remove_featured_from_loop() {
    return array(
        'cat'   => '-41',
        'paged' => get_query_var( 'paged') ? get_query_var( 'paged' ) : 1,
    );
}

That said, if this is for the main loop on the page, I would strongly advise to use pre_get_posts action to modify the main query rather than beans_loop_query_args which creates a new query. Here is more details about the difference between beans_loop_query_args and pre_get_posts.

Have fun,


Thank you,

It works perfectly ! I have tryed pre_get_posts but it doesn't work

add_action( 'pre_get_posts', 'example_query' );

function example_query( $query ) {

 // Modify the main query on the front end only.
 if ( $query->is_main_query() && ! is_admin() ) {
    $query->set( 'cat', -41 );
  }
}

php storm tell me that the methods is_main_query and set are not found...


Hey Alex,

Glad to hear the paged argument worked for your. Regarding the pre_get_posts action, where is your snippet located in your child theme?


HI Alexandra,

my question is off topic but how were you able toapply styling to your Offcanvas menu? Do I modify it in function.php and change the default style in ulit?

Write a reply

Login or register to write a reply, it's free!