Add taxonomies to latest_stories shortcode

This commit is contained in:
Tetrakern 2023-03-08 13:22:04 +01:00
parent d309213742
commit b3b868c7fd
5 changed files with 192 additions and 16 deletions

View File

@ -541,12 +541,22 @@ Renders a two-column grid of small cards, showing the latest four stories ordere
* **order:** Either `desc` (descending) or `asc` (ascending). Default `desc`.
* **orderby:** The default is `date`, but you can also use `modified` and [more](https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters).
* **stories:** Comma-separated list of post IDs, if you want to pick from a curated pool.
* **categories:** Comma-separated list of category names (case-insensitive), if you want to pick from a curated pool.
* **tags:** Comma-separated list of tag names (case-insensitive), if you want to pick from a curated pool.
* **fandoms:** Comma-separated list of fandom names (case-insensitive), if you want to pick from a curated pool.
* **genres:** Comma-separated list of genre names (case-insensitive), if you want to pick from a curated pool.
* **characters:** Comma-separated list of character names (case-insensitive), if you want to pick from a curated pool.
* **rel:** Relationship between different taxonomies, either `AND` or `OR`. Default `AND`.
* **class:** Additional CSS classes, separated by whitespace.
```
[fictioneer_latest_stories]
```
```
[fictioneer_latest_stories genres="adventure, cyberpunk" characters="Rebecca" rel="or"]
```
```
[fictioneer_latest_stories count="10" type="compact" author="Tetrakern" order="asc" orderby="modified" stories="1,2,3,5,8,13,21,34"]
```

View File

@ -392,6 +392,31 @@ Filters the query arguments in the `fictioneer_latest_posts` shortcode. The opti
* $taxonomies (array) Array of taxonomy arrays (names). Default empty.
* $rel (string) Relationship between taxonomies. Default `'AND'`.
* $classes (\[string]) Array of additional CSS classes. Default empty.
---
### `apply_filters( 'fictioneer_filter_latest_stories_query_args', $query_args, $args )`
Filters the query arguments in the `fictioneer_latest_stories` shortcode. The optional taxonomy arrays can include categories, tags, fandoms, genres, and characters.
**$query_args:**
* $post_type (string) `'fcn_story'`
* $post_status (string) `'publish'`
* $post__in (array) `$args['post_ids']`
* $meta_key (string) `'fictioneer_story_sticky'`
* $orderby (string) `'meta_value ' . $args['orderby']`
* $order (string) `$args['order']`
* $posts_per_page (int) `$args['count']`
* $no_found_rows (boolean) `true`
* $author_name (string|null) `$args['author']`
**$args:**
* $author (boolean|string) The author provided by the shortcode. Default `false`.
* $count (int) The number of posts provided by the shortcode. Default `1`.
* $orderby (string) Optional. Default `'date'`.
* $order (string) Optional. Default `'desc'`.
* $post_ids (\[string]) Array of post IDs. Default empty.
* $taxonomies (array) Array of taxonomy arrays (names). Default empty.
* $rel (string) Relationship between taxonomies. Default `'AND'`.
* $classes (\[string]) Array of additional CSS classes. Default empty.
---

View File

@ -270,6 +270,7 @@ function fictioneer_shortcode_latest_stories( $attr ) {
$orderby = $attr['orderby'] ?? 'date';
$post_ids = [];
$taxonomies = [];
$rel = 'AND';
$classes = [];
// Post IDs
@ -288,6 +289,26 @@ function fictioneer_shortcode_latest_stories( $attr ) {
$taxonomies['categories'] = fictioneer_explode_list( $attr['categories'] );
}
// Fandoms
if ( ! empty( $attr['fandoms'] ) ) {
$taxonomies['fandoms'] = fictioneer_explode_list( $attr['fandoms'] );
}
// Characters
if ( ! empty( $attr['characters'] ) ) {
$taxonomies['characters'] = fictioneer_explode_list( $attr['characters'] );
}
// Genres
if ( ! empty( $attr['genres'] ) ) {
$taxonomies['genres'] = fictioneer_explode_list( $attr['genres'] );
}
// Relation
if ( ! empty( $attr['rel'] ) ) {
$rel = strtolower( $attr['rel'] ) == 'or' ? 'OR' : $rel;
}
// Extra classes
if ( ! empty( $attr['class'] ) ) $classes[] = esc_attr( wp_strip_all_tags( $attr['class'] ) );
@ -299,6 +320,7 @@ function fictioneer_shortcode_latest_stories( $attr ) {
'orderby' => $orderby,
'post_ids' => $post_ids,
'taxonomies' => $taxonomies,
'relation' => $rel,
'classes' => $classes
);

View File

@ -8,11 +8,13 @@
* @subpackage Fictioneer
* @since 4.0
*
* @internal $args['count'] The number of posts provided by the shortcode.
* @internal $args['author'] The author provided by the shortcode.
* @internal $args['order'] Order of posts. Default 'desc'.
* @internal $args['orderby'] Sorting of posts. Default 'date'.
* @internal $args['post_ids'] Comma-separated list of story IDs. Overrides count.
* @internal $args['count'] The number of posts provided by the shortcode.
* @internal $args['author'] The author provided by the shortcode.
* @internal $args['order'] Order of posts. Default 'desc'.
* @internal $args['orderby'] Sorting of posts. Default 'date'.
* @internal $args['post_ids'] Array of post IDs. Default empty.
* @internal $args['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['classes'] Array of additional CSS classes. Default empty.
*/
?>
@ -20,8 +22,8 @@
// Prepare query
$query_args = array(
'post_type' => array( 'fcn_story' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_story',
'post_status' => 'publish',
'post__in' => $args['post_ids'],
'meta_key' => 'fictioneer_story_sticky',
'orderby' => 'meta_value ' . $args['orderby'],
@ -33,12 +35,70 @@ $query_args = array(
// Parameter for author?
if ( isset( $args['author'] ) && $args['author'] ) $query_args['author_name'] = $args['author'];
// Taxonomies?
if ( ! empty( $args['taxonomies'] ) ) {
$query_args['tax_query'] = [];
// Relationship?
if ( count( $args['taxonomies'] ) > 1 ) {
$query_args['tax_query']['relation'] = $args['relation'];
}
// Tags?
if ( ! empty( $args['taxonomies']['tags'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => $args['taxonomies']['tags']
);
}
// Categories?
if ( ! empty( $args['taxonomies']['categories'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => $args['taxonomies']['categories']
);
}
// Fandoms?
if ( ! empty( $args['taxonomies']['fandoms'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'fcn_fandom',
'field' => 'name',
'terms' => $args['taxonomies']['fandoms']
);
}
// Characters?
if ( ! empty( $args['taxonomies']['characters'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'fcn_character',
'field' => 'name',
'terms' => $args['taxonomies']['characters']
);
}
// Genres?
if ( ! empty( $args['taxonomies']['genres'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'fcn_genre',
'field' => 'name',
'terms' => $args['taxonomies']['genres']
);
}
}
// Apply filters
$query_args = apply_filters( 'fictioneer_filter_latest_stories_query_args', $query_args, $args );
// Query stories
$entries = new WP_Query( $query_args );
?>
<section class="small-card-block latest-stories _compact">
<section class="small-card-block latest-stories _compact <?php echo implode( ' ', $args['classes'] ); ?>">
<?php if ( $entries->have_posts() ) : ?>
<ul class="two-columns _collapse-on-mobile">

View File

@ -8,12 +8,13 @@
* @subpackage Fictioneer
* @since 4.0
*
* @internal $args['count'] The number of posts provided by the shortcode.
* @internal $args['author'] The author provided by the shortcode.
* @internal $args['order'] Order of posts. Default 'desc'.
* @internal $args['orderby'] Sorting of posts. Default 'date'.
* @internal $args['post_ids'] Comma-separated list of story IDs. Overrides count.
* @internal $args['class'] Additional classes.
* @internal $args['count'] The number of posts provided by the shortcode.
* @internal $args['author'] The author provided by the shortcode.
* @internal $args['order'] Order of posts. Default 'desc'.
* @internal $args['orderby'] Sorting of posts. Default 'date'.
* @internal $args['post_ids'] Array of post IDs. Default empty.
* @internal $args['taxonomies'] Array of taxonomy arrays. Default empty.
* @internal $args['classes'] Array of additional CSS classes. Default empty.
*/
?>
@ -21,8 +22,8 @@
// Prepare query
$query_args = array(
'post_type' => array( 'fcn_story' ),
'post_status' => array( 'publish' ),
'post_type' => 'fcn_story',
'post_status' => 'publish',
'post__in' => $args['post_ids'],
'meta_key' => 'fictioneer_story_sticky',
'orderby' => 'meta_value ' . $args['orderby'],
@ -34,6 +35,64 @@ $query_args = array(
// Parameter for author?
if ( isset( $args['author'] ) && $args['author'] ) $query_args['author_name'] = $args['author'];
// Taxonomies?
if ( ! empty( $args['taxonomies'] ) ) {
$query_args['tax_query'] = [];
// Relationship?
if ( count( $args['taxonomies'] ) > 1 ) {
$query_args['tax_query']['relation'] = $args['relation'];
}
// Tags?
if ( ! empty( $args['taxonomies']['tags'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => $args['taxonomies']['tags']
);
}
// Categories?
if ( ! empty( $args['taxonomies']['categories'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'name',
'terms' => $args['taxonomies']['categories']
);
}
// Fandoms?
if ( ! empty( $args['taxonomies']['fandoms'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'fcn_fandom',
'field' => 'name',
'terms' => $args['taxonomies']['fandoms']
);
}
// Characters?
if ( ! empty( $args['taxonomies']['characters'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'fcn_character',
'field' => 'name',
'terms' => $args['taxonomies']['characters']
);
}
// Genres?
if ( ! empty( $args['taxonomies']['genres'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'fcn_genre',
'field' => 'name',
'terms' => $args['taxonomies']['genres']
);
}
}
// Apply filters
$query_args = apply_filters( 'fictioneer_filter_latest_stories_query_args', $query_args, $args );
// Query stories
$entries = new WP_Query( $query_args );