Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use additional, to the multi select boxes, the categories from the current shown post. If select "All of the terms", it will be shown only categories from the current post.
<?php
/*
Plugin Name: AND-Same-Category Extension
Plugin URI: http://tiptoppress.com/downloads/term-and-category-based-posts-widget/
Description: Extension to add more sort possibilities for the premium widget Term and Category Based Posts Widget.
Author: TipTopPress
Version: 0.1
Author URI: http://tiptoppress.com
*/
namespace termCategoryPostsPro\SortByExtension;
// Don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Applied to the list of sort orders some new ones.
*
* @return array of sort orders
*
* @since 0.1
*/
function query_args ( $query_args, $this, $instance ) {
$possible_sorts = array(
'date' => __('Latest','categorypostspro'),
'comment_count' => __('Have more comments','categorypostspro'),
'rand' => __('Random','categorypostspro')
);
$possible_sorts = apply_filters('cpwp_possible_sorts',$possible_sorts,$this,$instance);
if (!isset($instance['sort_by']))
$instance['sort_by'] = 'date';
if ( isset($instance['sort_by']) && array_key_exists($instance['sort_by'], $possible_sorts) ) {
$sort_by = $instance['sort_by'];
} else {
$sort_by = 'date';
}
$sort_order = (isset( $instance['asc_sort_order'] ) && $instance['asc_sort_order']) ? 'ASC' : 'DESC';
// Exclude current post
$exclude_current_post = '';
if ((is_single() || is_singular()) && (isset( $instance['exclude_current_post'] ) && $instance['exclude_current_post'])) {
$exclude_current_post = get_the_ID();
}
$term_query = null;
if (isset($instance["terms"])) {
foreach ($instance["terms"] as $tax=>$terms) {
if (!in_array('0',$instance['terms'][$tax],true)) { // Any term for this taxonomy
$include_children = true;
if (isset($instance['no_child_terms'][$tax]) && $instance['no_child_terms'][$tax])
$include_children = false;
if ($term_query == null) { // the relationship setting need to be done only once
$term_query = array('relation' => 'OR');
if (isset($instance['tax_and_relationship']) && $instance['tax_and_relationship'])
$term_query = array('relation' => 'AND');
}
$operator = 'IN';
if (isset($instance['tax_term_inclusion'][$tax])) {
if ($instance['tax_term_inclusion'][$tax] == 'all')
$operator = 'AND';
else if ($instance['tax_term_inclusion'][$tax] == 'none')
$operator = 'NOT IN';
}
$term_query[] = array(
'taxonomy' => $tax,
'field' => 'term_id',
'terms' => array_merge($terms, wp_get_post_categories(get_the_ID())), // custom query Same Category
'include_children' => $include_children,
'operator' => $operator,
);
}
}
}
// Get array of post info.
if (isset($instance["num"]))
$args['showposts'] = (int) $instance["num"];
$args = array(
'orderby' => $sort_by,
'order' => $sort_order
);
if (is_array($term_query))
$args['tax_query'] = $term_query;
if ($exclude_current_post)
$args['post__not_in'] = array( $exclude_current_post );
if (array_key_exists('authors',$instance) && is_array($instance['authors']) && !in_array('0',$instance['authors'])) { // if not default authors
$args['author'] = join(',',$instance['authors']);
}
if (array_key_exists('post_types',$instance) && is_array($instance['post_types']) && !in_array('0',$instance['post_types'],true)) { // if not default authors
$args['post_type'] = $instance['post_types'];
} else
$args['post_type'] = 'any';
if (isset($instance["num"]))
$args['showposts'] = (int) $instance["num"];
if (isset($instance["offset"]) && ((int) $instance["offset"] > 1))
$args['offset'] = (int) $instance["offset"] - 1;
if (is_singular() && isset( $instance['exclude_current_post'] ) && $instance['exclude_current_post'])
$args['post__not_in'] = array(get_the_ID());
if( isset( $instance['hideNoThumb'] ) && $instance['hideNoThumb']) {
$args = array_merge( $args, array( 'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS' )
)
)
);
}
return $args;
}
add_filter( 'cpwp_query_args', __NAMESPACE__.'\query_args', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.