Skip to content

Instantly share code, notes, and snippets.

@stefanotorresi
Last active August 29, 2015 13:57
Show Gist options
  • Save stefanotorresi/9470776 to your computer and use it in GitHub Desktop.
Save stefanotorresi/9470776 to your computer and use it in GitHub Desktop.
wordpress custom archive hooks
<?php
/**
* archive list join clause
*/
function myArchiveJoin($join)
{
global $wpdb;
$join .= "
INNER JOIN {$wpdb->prefix}term_relationships
ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id)
INNER JOIN {$wpdb->prefix}term_taxonomy
ON ({$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id)
INNER JOIN {$wpdb->prefix}terms
ON {$wpdb->prefix}term_taxonomy.term_id = {$wpdb->prefix}terms.term_id
";
return $join;
}
add_filter('getarchives_join', 'myArchiveJoin' );
/**
* archive list where clause
*/
function myArchiveWhere($where)
{
global $wpdb;
$parameters = array( 'some-category', 'some-other-category' );
// escape happens later
$where .= " AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category' AND {$wpdb->prefix}terms.slug IN ('"
. implode("','", $parameters) . "')";
return $where;
}
add_filter('getarchives_where', 'myArchiveWhere');
/**
* archive page query
*/
function myArchiveTemplateQuery(WP_Query $query)
{
$terms = array('some-category', 'some-other-category');
if ( $query->is_main_query() && $query->is_archive() ) {
$query->set( 'tax_query', array(array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $terms
)));
}
return $query;
}
add_action( 'pre_get_posts', 'myArchiveTemplateQuery' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment