Skip to content

Instantly share code, notes, and snippets.

@rgadon107
Last active February 6, 2017 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgadon107/fc310c67e56c4140a3a1ce3591ae720f to your computer and use it in GitHub Desktop.
Save rgadon107/fc310c67e56c4140a3a1ce3591ae720f to your computer and use it in GitHub Desktop.
Archive template file for the Tours custom post type.
<?php
/**
* Archive template file for the Tours custom post type
*
* @package spiralWebDB\GenesisDeveloper
*
* @since 1.0.2
*
* @author Robert A. Gadon
*
* @link http://spiralwebdb.com
*
* @license GNU General Public License 2.0+
*/
namespace spiralWebDB\GenesisDeveloper;
add_action( 'pre_get_posts', __NAMESPACE__ .'\modify_query_vars_if_archive' );
/**
* Modify the default Query Vars for the custom post type.
*
* @since 1.0.2
*
* @param array $query_vars Array of query variables to modify for the custom post type.
*
* @return array $query_vars
*/
function modify_query_vars_if_archive( $query_vars = array() ) {
if( is_post_type_archive( $post_type = 'tours' ) ) {
$query_vars = array(
'post_type' => 'tours',
'order' => 'ASC',
'orderby' => 'menu_order',
);
foreach ( $query_vars as $var => $value ) {
set_query_var( $var, $value );
}
}
return $query_vars;
}
@rgadon107
Copy link
Author

rgadon107 commented Feb 6, 2017

Conditional is_post_type_archive() returns true on archive-tours.php page.

$query_vars returns the array values assigned above after looping through the array usingforeach.

An inspection of the global $wp_query variable after the array loop indicates that the$query_vars were properly modified.

However, the 'Simply Show Hooks' plugin tells me that the callback above is not preregistered to the pre_get_posts event. I'm stumped as to why.

@rgadon107
Copy link
Author

Make sure to add the file path name to the child theme's autoload.php file so it gets preloaded before initializing the Genesis framework (parent theme). Then the preregistered callback will get registered and the callback will fire.

@rgadon107
Copy link
Author

No need to include genesis(); at the end of the file if preloading the archive template in the child theme. Once the template file is preloaded, initialize Genesis and the template will run when called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment