<?php
/**
* ThemeREX Framework: WordPress utilities
*
* @package themerex
* @since themerex 1.0
*/
// Disable direct call
if ( ! defined( 'ABSPATH' ) ) { exit; }
/* Query manipulations
------------------------------------------------------------------------------------- */
// Add sorting parameter in query arguments
if (!function_exists('trx_donations_query_add_sort_order')) {
function trx_donations_query_add_sort_order($args, $orderby='date', $order='desc') {
$q = array();
$q['order'] = $order;
if ($orderby == 'comments') {
$q['orderby'] = 'comment_count';
} else if ($orderby == 'title' || $orderby == 'alpha') {
$q['orderby'] = 'title';
} else if ($orderby == 'rand' || $orderby == 'random') {
$q['orderby'] = 'rand';
} else {
$q['orderby'] = 'post_date';
}
foreach ($q as $mk=>$mv) {
if (is_array($args))
$args[$mk] = $mv;
else
$args->set($mk, $mv);
}
return $args;
}
}
// Add post type and posts list or categories list in query arguments
if (!function_exists('trx_donations_query_add_posts_and_cats')) {
function trx_donations_query_add_posts_and_cats($args, $ids='', $post_type='', $cat='', $taxonomy='') {
if (!empty($ids)) {
$args['post_type'] = empty($args['post_type'])
? (empty($post_type) ? array('post', 'page') : $post_type)
: $args['post_type'];
$args['post__in'] = explode(',', str_replace(' ', '', $ids));
} else {
$args['post_type'] = empty($args['post_type'])
? (empty($post_type) ? 'post' : $post_type)
: $args['post_type'];
$post_type = is_array($args['post_type']) ? $args['post_type'][0] : $args['post_type'];
if (!empty($cat)) {
$cats = !is_array($cat) ? explode(',', $cat) : $cat;
if (empty($taxonomy))
$taxonomy = 'category';
if ($taxonomy == 'category') { // Add standard categories
if (is_array($cats) && count($cats) > 1) {
$cats_ids = array();
foreach($cats as $c) {
$c = trim(chop($c));
if (empty($c)) continue;
if ((int) $c == 0) {
$cat_term = get_term_by( 'slug', $c, $taxonomy, OBJECT);
if ($cat_term) $c = $cat_term->term_id;
}
if ($c==0) continue;
$cats_ids[] = (int) $c;
$children = get_categories( array(
'type' => $post_type,
'child_of' => $c,
'hide_empty' => 0,
'hierarchical' => 0,
'taxonomy' => $taxonomy,
'pad_counts' => false
));
if (is_array($children) && count($children) > 0) {
foreach($children as $c) {
if (!in_array((int) $c->term_id, $cats_ids)) $cats_ids[] = (int) $c->term_id;
}
}
}
if (count($cats_ids) > 0) {
$args['category__in'] = $cats_ids;
}
} else {
if ((int) $cat > 0)
$args['cat'] = (int) $cat;
else
$args['category_name'] = $cat;
}
} else { // Add custom taxonomies
if (!isset($args['tax_query']))
$args['tax_query'] = array();
$args['tax_query']['relation'] = 'AND';
$args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'include_children' => true,
'field' => (int) $cats[0] > 0 ? 'id' : 'slug',
'terms' => $cats
);
}
}
}
return $args;
}
}
?>