PluginProbe
Pods – Custom Content Types and Fields / 2.8.23.5
Pods – Custom Content Types and Fields v2.8.23.5
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 All 42 releases
pods / src / Pods / Theme / WP_Query_Integration.php
WP_Query_Integration.php
99 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Pods\Theme;
4
5 use Exception;
6 use WP_Query;
7
8 /**
9 * WP_Query specific functionality.
10 *
11 * @since 2.8.0
12 */
13 class WP_Query_Integration {
14
15 /**
16 * Add the class hooks.
17 *
18 * @since 2.8.0
19 */
20 public function hook() {
21 add_action( 'pre_get_posts', [ $this, 'show_cpt_on_core_taxonomy_archive' ] );
22 }
23
24 /**
25 * Remove the class hooks.
26 *
27 * @since 2.8.0
28 */
29 public function unhook() {
30 remove_action( 'pre_get_posts', [ $this, 'show_cpt_on_core_taxonomy_archive' ] );
31 }
32
33 /**
34 * Show Custom Post Type on core Taxonomy archive.
35 *
36 * @since 2.8.0
37 *
38 * @param WP_Query $query The WP_Query instance.
39 */
40 public function show_cpt_on_core_taxonomy_archive( $query ) {
41 // Skip on admin screens.
42 if ( is_admin() ) {
43 return;
44 }
45
46 // Skip if not on the archive we want.
47 if (
48 ! empty( $query->query_vars['suppress_filters'] )
49 || ! $query->is_main_query()
50 || $query->is_404()
51 || (
52 ! $query->is_category()
53 && ! $query->is_tag()
54 )
55 ) {
56 return;
57 }
58
59 $object = $query->get_queried_object();
60
61 if ( ! isset( $object->taxonomy ) ) {
62 return;
63 }
64
65 $taxonomy = $object->taxonomy;
66
67 // Find all CPT that have this taxonomy set.
68 $api = pods_api();
69
70 try {
71 $post_types_to_show = $api->load_pods( [
72 'args' => [
73 'archive_show_in_taxonomies_' . $taxonomy => 1,
74 ],
75 'names' => true,
76 ] );
77 } catch ( Exception $exception ) {
78 return;
79 }
80
81 $post_types_to_show = array_keys( $post_types_to_show );
82
83 $existing_post_types = $query->get( 'post_type' );
84
85 if ( empty( $existing_post_types ) ) {
86 $existing_post_types = [
87 'post',
88 ];
89 } elseif ( ! is_array( $existing_post_types ) ) {
90 $existing_post_types = (array) $existing_post_types;
91 }
92
93 $post_types_to_show = array_unique( array_merge( $existing_post_types, $post_types_to_show ) );
94
95 $query->set( 'post_type', $post_types_to_show );
96 }
97
98 }
99