Skip to content

Instantly share code, notes, and snippets.

@jimmyandrade
Last active March 17, 2017 17:46
Show Gist options
  • Save jimmyandrade/515c66b538a5e79e87c5 to your computer and use it in GitHub Desktop.
Save jimmyandrade/515c66b538a5e79e87c5 to your computer and use it in GitHub Desktop.
Author like get posts
<?php
private static function tax_query_name_like( $taxonomy, $term ) {
global $wpdb;
$statement = $wpdb->prepare(
"
SELECT term_taxonomy_id
FROM $wpdb->terms
JOIN $wpdb->term_taxonomy
USING (term_id)
WHERE name
LIKE %s
AND taxonomy = %s
", '%' . $term . '%', $taxonomy );
return $wpdb->get_col( $statement );
}
public static function pre_get_posts( $query ) {
/* @var $author_like string Retorna o nome do autor, caso o usuario tenha usado a busca avancada */
$author_like = filter_input( INPUT_GET, 'author_like', FILTER_SANITIZE_STRING );
/* @var $author_query_string string As palavras-chave da busca normal ou o nome do autor da avancada */
$author_query_string = empty( $author_like ) ? get_search_query() : $author_like;
/* @var $term_ids array Um vetor com os IDs de todos os autores que possuam nome parecido com o buscado */
$term_ids = static::tax_query_name_like( 'author', $author_query_string );
if ( count( $term_ids ) > 0 ) {
$author_query_args = array(
array(
'taxonomy' => 'author',
'field' => 'term_id',
'include_children' => false,
'operator' => 'IN',
'terms' => $term_ids
)
);
$query->set( 'tax_query', $author_query_args );
}
return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment