Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created December 13, 2018 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/b1b2c403a39ebb8b907a328ab10148ac to your computer and use it in GitHub Desktop.
Save tommcfarlin/b1b2c403a39ebb8b907a328ab10148ac to your computer and use it in GitHub Desktop.
[WordPress] Custom Archive Templates: A Short Guide
<?php
$eventQuery = new \WP_Query([
'post_type' => 'acme-events',
'post_status' => 'publish',
'orderby' => 'meta_value',
'order' => 'desc',
'meta_key' => 'acme-event-start-date-time',
'posts_per_archive_page' => 5,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
]);
<?php
add_action('pre_get_posts', 'setCustomQueryVariable');
public function setCustomQueryVariable($query)
{
if (is_admin() || !is_archive()) {
return;
}
if ($query->is_archive('acme-events')) {
set_query_var('posts_per_page', 5);
}
}
<?php wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment