Skip to content

Instantly share code, notes, and snippets.

@wellingtonngallo
Created September 15, 2017 18:04
Show Gist options
  • Save wellingtonngallo/c659130fe66c83b175694316dee9812e to your computer and use it in GitHub Desktop.
Save wellingtonngallo/c659130fe66c83b175694316dee9812e to your computer and use it in GitHub Desktop.
<?php
function wpes_ajusta_leitura_e_filtros( $query ) {
if ( $query->is_main_query() && ( is_tax( 'tipo-material' ) || is_post_type_archive("material") ) && !is_admin()) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'desc' );
$query->set( 'posts_per_page', '12' );
if(isset($_GET['sol']) || isset($_GET['prd'])) {
$meta = array(
'relation' => 'OR'
);
$ids = explode(",", $_GET['sol']);
foreach ($ids as $value) {
if(!$value || $value == "-1")
continue;
$meta[] = array(
'key' => 'es-material-relacao',
'value' => '"' . $value . '"',
'compare' => 'LIKE'
);
}
if(isset($_GET['prd'])) {
$ids = explode(",", $_GET['prd']);
foreach ($ids as $value) {
if(!$value || $value == "-1")
continue;
$meta[] = array(
'key' => 'es-material-relacao',
'value' => '"' . $value . '"',
'compare' => 'LIKE'
);
}
}
$query->set('meta_query', $meta);
}
if(isset($_GET['tp'])) {
$tipos = array();
$ids = explode(",", $_GET['tp']);
foreach ($ids as $value) {
if(!$value || $value == "-1")
continue;
$tipos[] = $value;
}
if(count($tipos)) {
$query->set('tax_query', array(
array(
'taxonomy' => 'tipo-material',
'field' => 'id',
'terms' => $tipos
)));
}
}
}
}
add_action( 'pre_get_posts', 'wpes_ajusta_leitura_e_filtros' );
function wpes_template_chooser($template){
global $wp_query;
if( $wp_query->is_search && $_GET['post_type'] == 'material' ) {
$wp_query->set('post_type', 'material');
return locate_template('archive-material.php');
}
return $template;
}
add_filter('template_include', 'wpes_template_chooser');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment