A
A
Alexey_Ruzheinikov2021-10-25 12:19:55
WordPress
Alexey_Ruzheinikov, 2021-10-25 12:19:55

How to change the number of visible posts of a new post type in the WP admin?

Created fields in Redux for new post types "Reviews" and "News", which display the number of posts displayed on the archive pages of these posts. For "Reviews" in Redux, I chose to display 4 posts, for "News" - 2.
I wrote the code for this in functions.php.

function legal_advisory_posts_per_archivepage($query){
  global $legal_advisory;
  $posts_per_page_testy = -1;
  $posts_per_page_news = -1;

  if($legal_advisory['testimonial_posts']){$posts_per_page_testy = $legal_advisory['testimonial_posts'];} 
  if($legal_advisory['newspostsperpage']){$posts_per_page_news = $legal_advisory['newspostsperpage'];}
  if(is_post_type_archive('testimonial')){
    $query->set('posts_per_page', $posts_per_page_testy);
  }
  if(is_post_type_archive('news')){
    $query->set('posts_per_page', $posts_per_page_news);
  }
}
add_action('pre_get_posts','legal_advisory_posts_per_archivepage');


Now everything works as expected, on the archive page "Reviews" - shows 4 entries and on the "News" page - 2 entries.
There was a problem that now the same number of records are displayed in the visible ones in the admin panel.
That is 4 entries for "reviews". And it is necessary that all records were shown.
617675ec31687022736373.png
And 2 entries for "News", but we also need all the entries in the admin panel.
6176765239811514719273.png
What needs to be added in the code so that it does not affect the admin panel and all the entries are shown in the admin panel itself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Litvinenko, 2021-10-25
@Alexey_Ruzheinikov

Wrap it up like this

if (!is_admin() && $query->is_main_query()) {

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question