$related_post_per_page , 'paged' => 1, 'ignore_sticky_posts' => true, 'post_type' => $related_posttype, 'orderby' => array( 'menu_order' => 'ASC' , 'post_date' => 'DESC'), 'post_status' => 'publish', 'post__not_in'=>array(get_the_id()), 'ignore_sticky_posts' => 1 ); $argsList[ 'meta_query'] = array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ) ); $secondaryQuery = $argsList; // used if we have too few results /** * * Check if we have a taxonomy result and add to query * */ if('category' == $related_taxonomy){ $terms = get_the_category( get_the_id() ); } else { $terms = get_the_terms( get_the_id() , $related_taxonomy); } $term_ids = false; if( !is_wp_error( $terms ) ) { if(is_array($terms)) { $term_ids = wp_list_pluck($terms,'term_id'); if ($term_ids) { // calculate the url for first taxonomy $termid = (int)$terms[0]->term_id; $temp_link = get_term_link($termid, $related_taxonomy); if(!is_wp_error( $temp_link )){ $link = $temp_link; } // add tax to query $argsList['tax_query'] = array( array( 'taxonomy' => $related_taxonomy, 'field' => 'id', 'terms' => $term_ids, 'operator'=> 'IN' ) ); } } } /** * * Execute query * */ $the_query = new WP_Query($argsList); $total_results = $the_query->found_posts; $found_array = array( get_the_id() ); $count = $total_results; /** * Secondary query to warranty a minimum amount of contents */ $have_alternative = false; if($total_results < $related_post_per_page){ $secondaryQuery['post__not_in'] = array_merge($found_array, array(get_the_id())); $secondaryQuery['posts_per_page'] = $related_post_per_page - $total_results; $secondaryQuery['ignore_sticky_posts'] = 1; $secondaryQuery[ 'meta_query'] = array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ) ); $the_query_secondary = new WP_Query($secondaryQuery); if($the_query_secondary->have_posts()){ $have_alternative = true; $count = $count + $the_query_secondary->found_posts; } } if ( $the_query->have_posts() || $have_alternative ) : $count = $the_query->post_count; switch ($related_posttype){ case "vlogger_serie": $template = 'phpincludes/part-archive-item-card-simple'; break; default: $template = 'phpincludes/part-archive-item-medium'; } ?>