-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from ryanwelcher/feature/taxonomy-control
Taxonomy Builder
- Loading branch information
Showing
20 changed files
with
686 additions
and
34 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Manage parsing the meta query information | ||
*/ | ||
|
||
namespace AdvancedQueryLoop\Traits; | ||
|
||
trait Tax_Query { | ||
|
||
public function process_tax_query() { | ||
$this->custom_args['tax_query'] = $this->parse_tax_query( $this->custom_params['tax_query'] ); | ||
} | ||
|
||
public function parse_tax_query( $queries ) { | ||
$tax_query = []; | ||
// Don't process empty array of queries. | ||
if ( isset( $queries['queries'] ) && count( $queries['queries'] ) > 0 ) { | ||
// Handle the relation parameter. | ||
if ( isset( $queries['relation'] ) && count( $queries['queries'] ) > 1 ) { | ||
$tax_query['relation'] = $queries['relation']; | ||
} | ||
// Loop the queries | ||
foreach ( $queries['queries'] as $query ) { | ||
if ( isset( $query['taxonomy'] ) && isset( $query['terms'] ) && count( $query['terms'] ) > 0 ) { | ||
$processed_query = array_filter( $query, fn( $key ) => 'id' !== $key, ARRAY_FILTER_USE_KEY ); | ||
$processed_query['include_children'] = filter_var( $query['include_children'], FILTER_VALIDATE_BOOLEAN ); | ||
$processed_query['terms'] = [ ...array_map( fn( $term ) => get_term_by( 'name', $term, $query['taxonomy'] )->term_id, $query['terms'] ) ]; | ||
$tax_query[] = $processed_query; | ||
} | ||
} | ||
} | ||
return $tax_query; | ||
} | ||
} | ||
|
||
/** | ||
* Example complex query: | ||
* $tax_query = array( | ||
* 'relation' => 'OR', | ||
* array( | ||
* 'taxonomy' => 'category', | ||
* 'field' => 'slug', | ||
* 'terms' => array( 'quotes' ), | ||
* ), | ||
* array( | ||
* 'taxonomy' => 'tag', | ||
* 'field' => 'slug', | ||
* 'terms' => array( 2 ), | ||
* ), | ||
* array( | ||
* 'relation' => 'AND', | ||
* array( | ||
* 'taxonomy' => 'post_format', | ||
* 'field' => 'slug', | ||
* 'terms' => array( 'post-format-quote' ), | ||
* ), | ||
* array( | ||
* 'taxonomy' => 'category', | ||
* 'field' => 'slug', | ||
* 'terms' => array( 'wisdom' ), | ||
* ), | ||
* ), | ||
* ); | ||
*/ |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Taxonomy relation functions | ||
* | ||
* @package AdvancedQueryLoop\Taxonomy | ||
*/ | ||
|
||
namespace AdvancedQueryLoop\Taxonomy; | ||
|
||
function convert_names_to_ids( $names, $tax ) { | ||
$rtn = []; | ||
foreach ( $names as $name ) { | ||
$term = get_term_by( 'name', $name, $tax ); | ||
if ( $term ) { | ||
$rtn[] = $term->term_id; | ||
} | ||
} | ||
return $rtn; | ||
} | ||
|
||
function parse_taxonomy_query( $tax_query_data ) { | ||
return [ | ||
[ | ||
'taxonomy' => $tax_query_data['taxonomy'], | ||
'terms' => convert_names_to_ids( $tax_query_data['terms'], $tax_query_data['taxonomy'] ), | ||
'include_children' => ( ! isset( $tax_query_data['include_children'] ) || 'true' === $tax_query_data['include_children'] ) ? true : false, | ||
'operator' => $tax_query_data['operator'], | ||
], | ||
]; | ||
} |
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.