Skip to content

Commit 8537ddb

Browse files
authored
fix: add failsafe to ensure that Sponsors queries contain only sponsors CPT (#221)
* fix: add failsafe to ensure that Sponsors queries contain only sponsors CPT * fix: phpcs error
1 parent 4d53dc0 commit 8537ddb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

includes/class-core.php

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function __construct() {
5050
add_action( 'init', [ __CLASS__, 'init' ] );
5151
add_filter( 'newspack_ads_should_show_ads', [ __CLASS__, 'suppress_ads' ], 10, 2 );
5252
add_filter( 'newspack_ads_ad_targeting', [ __CLASS__, 'ad_targeting' ], 10, 2 );
53+
add_action( 'pre_get_posts', [ __CLASS__, 'ensure_only_sponsors' ] );
5354
}
5455

5556
/**
@@ -518,6 +519,18 @@ public static function create_shadow_term( $post ) {
518519

519520
return $new_term;
520521
}
522+
523+
/**
524+
* Failsafe to ensure that sponsors queries only fetch sponsors post types.
525+
* Other plugins may have query filters to add other post types to queries.
526+
*
527+
* @param WP_Query $query Query.
528+
*/
529+
public static function ensure_only_sponsors( $query ) {
530+
if ( boolval( $query->get( 'is_sponsors' ) ) ) {
531+
$query->set( 'post_type', self::NEWSPACK_SPONSORS_CPT ); // phpcs:ignore WordPressVIPMinimum.Hooks.PreGetPosts.PreGetPosts
532+
}
533+
}
521534
}
522535

523536
Core::instance();

includes/theme-helpers.php

+2
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ function( $sponsor ) use ( $id ) {
285285
function get_related_post( $slug ) {
286286
$related_post = new \WP_Query(
287287
[
288+
'is_sponsors' => 1,
288289
'post_type' => Core::NEWSPACK_SPONSORS_CPT,
289290
'posts_per_page' => 1,
290291
'post_status' => 'publish',
@@ -333,6 +334,7 @@ function get_sponsor_posts_for_terms( $terms ) {
333334

334335
$sponsor_posts = new \WP_Query(
335336
[
337+
'is_sponsors' => 1,
336338
'post_type' => Core::NEWSPACK_SPONSORS_CPT,
337339
'posts_per_page' => 100,
338340
'post_status' => 'publish',

0 commit comments

Comments
 (0)