Last active
March 4, 2020 10:14
-
-
Save ThemeGravity/47dfddae8eec69927724ead8d9bd404b to your computer and use it in GitHub Desktop.
Add multiply categories, tags and relation
This file contains hidden or 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
public static function render_file_links( $attrs ) { | |
$files = array(); | |
$post_args = array( | |
'post_type' => models\File::$cpt, | |
'post_status' => 'publish', | |
'posts_per_page' => isset( $attrs['limit'] ) && 0 < $attrs['limit'] ? $attrs['limit'] : - 1 | |
); | |
if ( isset( $attrs['category'] ) && ! empty( $attrs['category'] ) ) { | |
$relation = isset( $attrs['category-relation'] ) && in_array( strtoupper( $attrs['category-relation'] ), array( | |
'IN', | |
'AND' | |
) ) ? strtoupper( $attrs['category-relation'] ) : 'IN'; | |
if ( strpos( $attrs['category'], ',' ) !== false ) { | |
$terms = explode( ',', trim( $attrs['category'] ) ); | |
} else { | |
$terms = trim( $attrs['category'] ); | |
} | |
if ( ! is_numeric( $attrs['category'] ) ) { // Category is a slug or name | |
$post_args = \array_merge( | |
array( | |
'tax_query' => array( | |
'relation' => 'OR', | |
array( | |
'relation' => $relation, | |
'taxonomy' => models\File::$file_category_ctax, | |
'field' => 'name', | |
'terms' => $terms, | |
'operator' => $relation | |
), | |
array( | |
'taxonomy' => models\File::$file_category_ctax, | |
'field' => 'slug', | |
'terms' => $terms, | |
'operator' => $relation | |
) | |
) | |
), | |
$post_args ); | |
} else { // Category is an ID | |
$post_args = \array_merge( | |
array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => models\File::$file_category_ctax, | |
'field' => 'term_id', | |
'terms' => $terms, | |
'operator' => $relation | |
) | |
) | |
), | |
$post_args | |
); | |
} | |
} elseif ( isset( $attrs['tag'] ) && ! empty( $attrs['tag'] ) ) { | |
$relation = isset( $attrs['tag-relation'] ) && in_array( strtoupper( $attrs['tag-relation'] ), array( | |
'IN', | |
'AND' | |
) ) ? strtoupper( $attrs['tag-relation'] ) : 'IN'; | |
if ( strpos( $attrs['tag'], ',' ) !== false ) { | |
$terms = explode( ',', trim( $attrs['tag'] ) ); | |
} else { | |
$terms = trim( $attrs['tag'] ); | |
} | |
if ( ! is_numeric( $attrs['tag'] ) ) { | |
$post_args = \array_merge( | |
array( | |
'tax_query' => array( | |
'relation' => 'OR', | |
array( | |
'taxonomy' => models\File::$file_tag_ctax, | |
'field' => 'name', | |
'terms' => $terms, | |
'operator' => $relation | |
), | |
array( | |
'taxonomy' => models\File::$file_tag_ctax, | |
'field' => 'slug', | |
'terms' => $terms, | |
'operator' => $relation | |
) | |
) | |
), | |
$post_args ); | |
} else { // Tag is an ID | |
$post_args = \array_merge( | |
array( | |
'tax_query' => array( | |
array( | |
'taxonomy' => models\File::$file_tag_ctax, | |
'field' => 'term_id', | |
'terms' => $terms, | |
'operator' => $relation | |
), | |
) | |
), | |
$post_args | |
); | |
} | |
} | |
if ( isset( $attrs['class'] ) ) { | |
$link_class = $attrs['class']; | |
} | |
$file_posts = \get_posts( $post_args ); | |
foreach ( $file_posts as $post ) { | |
$files[] = new models\File( $post->ID ); | |
} | |
\ob_start(); | |
require( base\VIEWS_PATH . '/files/file_links.php' ); | |
return \ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment