Created
June 2, 2023 14:49
-
-
Save kingkool68/efa01ecff91bc722cd3f667ae509a37b to your computer and use it in GitHub Desktop.
Adds /all/ URL after a post type archive to be able to show all items for that archive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Show All Post Type Archives | |
* Description: Add support for /all/ after a post type archive | |
* Version: 0.0.1 | |
* Plugin URI: https://gist.github.com/kingkool68/efa01ecff91bc722cd3f667ae509a37b | |
* Author: Russell Heimlich | |
* Author URI: https://github.com/kingkool68 | |
*/ | |
class RH_Show_All_Archives { | |
/** | |
* The query var we use to determine if the request is an "all" post type archive | |
* | |
* @var string | |
*/ | |
public static $query_var = 'show_all_archive'; | |
/** | |
* Get an instance of this class | |
*/ | |
public static function get_instance() { | |
static $instance = null; | |
if ( null === $instance ) { | |
$instance = new static(); | |
$instance->setup_actions(); | |
$instance->setup_filters(); | |
} | |
return $instance; | |
} | |
/** | |
* Hook into WordPress via actions | |
*/ | |
public function setup_actions() { | |
add_action( 'pre_get_posts', array( $this, 'action_pre_get_posts' ) ); | |
} | |
/** | |
* Hook into WordPress via filters | |
*/ | |
public function setup_filters() { | |
add_filter( 'register_post_type_args', array( $this, 'filter_register_post_type_args' ), 10, 2 ); | |
add_filter( 'query_vars', array( $this, 'filter_query_vars' ) ); | |
add_filter( 'generate_rewrite_rules', array( $this, 'filter_generate_rewrite_rules' ) ); | |
add_filter( 'template_include', array( $this, 'filter_template_include' ) ); | |
} | |
/** | |
* Modify the main loop on the "Show All" post type archive page to show all of the posts in the query | |
* | |
* @param WP_Query $the_query The query to modify | |
*/ | |
public function action_pre_get_posts( $the_query ) { | |
if ( ! $the_query->is_main_query() || ! static::is_show_all_archive() ) { | |
return; | |
} | |
$the_query->set( 'posts_per_page', 999 ); | |
} | |
/** | |
* Add the "all-archive" support to a pre-determined set of post types | |
* | |
* @param array $args Register post type arguments to modify | |
* @param string $post_type The post type being modified | |
*/ | |
public function filter_register_post_type_args( $args = array(), $post_type = '' ) { | |
$allowed_post_types = array( 'gallery' ); | |
if ( in_array( $post_type, $allowed_post_types, true ) ) { | |
$args['supports'][] = 'all-archive'; | |
} | |
return $args; | |
} | |
/** | |
* Register our query var with WordPress | |
* | |
* @param array $query_vars The list of query vars to modify | |
*/ | |
public function filter_query_vars( $query_vars = array() ) { | |
$query_vars[] = static::$query_var; | |
return $query_vars; | |
} | |
/** | |
* Add a /all/ rewrite rule to each post type archive that supports 'all-archive' | |
* | |
* @param WP_Rewrite $wp_rewrite The WordPress Rewrite object to modify | |
*/ | |
public function filter_generate_rewrite_rules( $wp_rewrite ) { | |
$new_rules = array(); | |
$post_type_names = static::get_supported_post_types(); | |
foreach ( $post_type_names as $post_type_name ) { | |
$post_type_obj = get_post_type_object( $post_type_name ); | |
$the_slug = $post_type_obj->rewrite['slug']; | |
$key = $the_slug . '/all/?$'; | |
$value = 'index.php?post_type=' . $post_type_name . '&' . static::$query_var . '=1'; | |
$new_rules[ $key ] = $value; | |
} | |
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; | |
} | |
/** | |
* Add support for loading different templates for the /all/ archive | |
* | |
* @param string $orig_template The originally requested template path | |
*/ | |
public function filter_template_include( $orig_template = '' ) { | |
if ( static::is_show_all_archive() ) { | |
$post_type = get_post_type(); | |
$template_names = array( | |
'archive-' . $post_type . '-all.php', | |
'archive-all.php', | |
'archive-' . $post_type . '.php', | |
'archive.php', | |
'index.php', | |
); | |
$new_template = locate_template( $template_names ); | |
if ( ! empty( $new_template ) ) { | |
return $new_template; | |
} | |
} | |
return $orig_template; | |
} | |
/** | |
* Get all of the post types that support the 'all-archive' feature | |
*/ | |
public static function get_supported_post_types() { | |
$post_types = get_post_types_by_support( 'all-archive' ); | |
return apply_filters( 'rh_show_all_archives/post_types', $post_types ); | |
} | |
/** | |
* Helper to determine if request is for an "all" post type archive | |
* | |
* @return boolean | |
*/ | |
public static function is_show_all_archive() { | |
return ( get_query_var( static::$query_var ) ); | |
} | |
} | |
RH_Show_All_Archives::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment