Skip to content

Commit c1b93c0

Browse files
fix: use main query for archive
1 parent a63467a commit c1b93c0

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

inc/Core.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private function run_hooks() {
5555
add_action( 'after_setup_theme', array( $this, 'setup' ) );
5656
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
5757
add_action( 'enqueue_block_editor_assets', array( $this, 'add_editor_styles' ) );
58+
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
5859
}
5960

6061
/**
@@ -107,4 +108,20 @@ public function enqueue() {
107108
public function add_editor_styles() {
108109
Assets_Manager::enqueue_style( Assets_Manager::ASSETS_SLUGS['editor-css'], 'editor' );
109110
}
111+
112+
/**
113+
* Modify the main query before it is executed.
114+
*
115+
* @param \WP_Query $query The WP_Query instance (passed by reference).
116+
*/
117+
public function pre_get_posts( $query ) {
118+
// Only modify frontend main query.
119+
if ( is_admin() || ! $query->is_main_query() ) {
120+
return;
121+
}
122+
123+
if ( $query->is_archive() ) {
124+
$query->set( 'posts_per_page', 4 );
125+
}
126+
}
110127
}

templates/archive.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"64px","bottom":"64px"}}},"layout":{"inherit":true,"type":"constrained"},"otterConditions":[]} -->
2525
<div class="wp-block-group alignfull" style="padding-top:64px;padding-bottom:64px">
26-
<!-- wp:query {"queryId":4,"query":{"perPage":"4","pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false},"displayLayout":{"type":"list","columns":2},"layout":{"inherit":true}} -->
26+
<!-- wp:query {"queryId":4,"query":{"perPage":"4","pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true},"displayLayout":{"type":"list","columns":2},"layout":{"inherit":true}} -->
2727
<div class="wp-block-query">
2828
<!-- wp:post-template -->
2929
<!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|60","bottom":"var:preset|spacing|60"}}},"backgroundColor":"ti-bg-alt","layout":{"inherit":true,"type":"constrained"},"otterConditions":[]} -->

0 commit comments

Comments
 (0)