Instantly share code, notes, and snippets.
Last active
August 7, 2020 23:04
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save DanielFloeter/221f3b5cf786170154391af3774b62df to your computer and use it in GitHub Desktop.
Select post types and show related term and categories from the current shown post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Related Terms and Category Add-on | |
Plugin URI: http://tiptoppress.com/downloads/term-and-category-based-posts-widget/ | |
Description: Add-on to select related terms and categories for the premium widget Term and Category Based Posts Widget. | |
Author: TipTopPress | |
Version: 0.2 | |
Author URI: http://tiptoppress.com | |
*/ | |
namespace termCategoryPostsPro\RelatedAddon; | |
// 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, $that, $instance ) { | |
$possible_sorts = array( | |
'date' => esc_html__( 'Latest', 'categorypostspro' ), | |
'comment_count' => esc_html__( 'Have more comments', 'categorypostspro' ), | |
'rand' => esc_html__( 'Random', 'categorypostspro' ), | |
); | |
$possible_sorts = apply_filters( 'cpwp_possible_sorts', $possible_sorts, $that, $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'; | |
$sort_order = apply_filters( 'cpwp_sort_order', $sort_order, $that, $instance ); | |
// 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 ) { | |
$include_children = true; | |
if ( isset( $instance['no_child_terms'][ $tax ] ) && $instance['no_child_terms'][ $tax ] ) { | |
$include_children = false; | |
} | |
if ( null === $term_query ) { // 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 ( 'all' === $instance['tax_term_inclusion'][ $tax ] ) { | |
$operator = 'AND'; | |
} elseif ( 'none' === $instance['tax_term_inclusion'][ $tax ] ) { | |
$operator = 'NOT IN'; | |
} | |
} | |
if ( isset( $instance['show_terms_randomly'][ $tax ] ) && $instance['show_terms_randomly'][ $tax ] ) { | |
if ( in_array( '0', $instance['terms'][ $tax ], true ) ) { | |
$terms = get_terms( $tax, $args ); | |
$terms = $terms[ array_rand( $terms, 1 ) ]->term_id; | |
} else { | |
$terms = $terms[ array_rand( $terms, 1 ) ]; | |
} | |
} | |
$related_terms = get_the_terms(get_the_ID(), $tax); | |
$related_terms_id = array(); | |
if ($related_terms) { | |
foreach ($related_terms as $related_term) { | |
$related_terms_id[] = $related_term->term_id; | |
} | |
} | |
$term_query[] = array( | |
'taxonomy' => $tax, | |
'field' => 'term_id', | |
'terms' => array_merge($terms, $related_terms_id), // custom query: AND 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, | |
'ignore_sticky_posts' => 1, // Make sure we do not get stickies out of 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'], true ) ) { // 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 post_type. | |
$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() ); | |
} | |
$non_default_valid_status = array( | |
'publish', | |
'future', | |
'publish,future', | |
'private', | |
'private,publish', | |
'private,publish,future', | |
); | |
if ( isset( $instance['status'] ) && in_array( $instance['status'], $non_default_valid_status, true ) ) { | |
$args['post_status'] = $instance['status']; | |
} | |
if ( isset( $instance['post_types'] ) && in_array( 'attachment', $instance['post_types'], true ) ) { | |
$args['post_status'] .= ',inherit'; // used for media/attachments | |
} | |
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