Commit 9240c4fa authored by Rafael Chaves Freitas's avatar Rafael Chaves Freitas
Browse files

Função para retornar termos de uma taxonomia pela ordem de utilização

parent 81633343
......@@ -12,4 +12,63 @@ function template_part($template_name, $params = []){
extract($params);
include $template_filename;;
}
/**
* Retorna uma lista de termos ordenados pela data de utilização.
*
* @param string $taxonomy
* @param integer $limit
* @param integer $paged
* @return \WP_Term[]
*/
function get_terms_order_by_posts_publish_date(string $taxonomy, int $limit = 10, int $paged = 1){
global $wpdb;
if(false) $wpdb = new \wpdb(1,2,3,4);
$offset = $limit * ($paged - 1);
$sql = "
SELECT
t.term_id,
t.name,
t.slug,
tx.term_taxonomy_id,
tx.taxonomy,
tx.description,
tx.parent,
tx.count
FROM
$wpdb->terms t
LEFT JOIN $wpdb->term_taxonomy tx ON tx.term_id = t.term_id
LEFT JOIN $wpdb->term_relationships tr ON tr.term_taxonomy_id = tx.term_taxonomy_id
LEFT JOIN $wpdb->posts p ON p.ID = tr.object_id
WHERE
tx.taxonomy = '$taxonomy' AND
p.post_type = 'coluna' AND
tr.object_id IN (
SELECT
MAX(object_id)
FROM
$wpdb->term_relationships
WHERE
term_taxonomy_id = tr.term_taxonomy_id
)
GROUP BY tx.term_taxonomy_id
ORDER BY p.post_date DESC
LIMIT $limit
OFFSET $offset
";
$results = $wpdb->get_results($sql);
$terms = array_map(function($term){ return new \WP_Term($term); }, $results);
return $terms;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment