Advertisement
phpface

Filter Widget Post query

Sep 20th, 2022 (edited)
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. /**
  2.  *
  3.  * Filter Widget Post query
  4.  *
  5.  * @param array $query_args
  6.  * @param array $instance
  7.  *
  8.  */
  9. add_filter( 'streamtube/core/widget/posts/query_args', function(  $query_args, $instance ){
  10.  
  11.     /**
  12.      *
  13.      * Holds the custom taxonomy slug(s), for instance: custom-taxonomy-slug or array( 'custom-taxonomy-slug' )
  14.      *
  15.      * @var string|array
  16.      */
  17.     $taxonomy = 'custom-taxonomy-slug';
  18.  
  19.     // Holds the widget ID
  20.     $widget_id = 'posts-widget-9';
  21.  
  22.     // Holds relation
  23.     $relation = 'AND'; // or OR
  24.  
  25.     if( array_key_exists( 'id', $instance ) && $instance['id'] == $widget_id ){
  26.  
  27.         if( $query_args['tax_query'] ){
  28.  
  29.             unset( $query_args['tax_query']['relation'] );
  30.  
  31.             if( is_string( $taxonomy ) ){
  32.                 $taxonomy = array( $taxonomy );
  33.             }
  34.  
  35.             $tax_query = compact( 'relation' );
  36.  
  37.             for ( $i=0; $i < count( $query_args['tax_query'] ); $i++) {
  38.                 if( is_array( $query_args['tax_query'][$i] ) ){
  39.                     if( in_array( $query_args['tax_query'][$i]['taxonomy'], $taxonomy ) ){
  40.                         $tax_query[] = $query_args['tax_query'][$i];
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.  
  46.         if( $tax_query ){
  47.             $query_args = array_merge( $query_args, compact( 'tax_query' ) );
  48.         }
  49.     }
  50.  
  51.     return $query_args;
  52.  
  53. }, 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement