Last active
December 21, 2022 08:26
-
-
Save AchalJ/016aab4548d48ffa600081f8eecd3387 to your computer and use it in GitHub Desktop.
Posts matching filters fix for Beaver Builder Posts Grid + Content Grid
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
<?php // <- Ignore this | |
// Copy the code below and paste it to your child theme's functions.php file | |
add_filter( 'fl_builder_loop_query_args', function( $args ) { | |
$settings = $args['settings']; | |
$post_type = $args['post_type']; | |
$post__in = $post__not_in = array(); | |
foreach ( (array) $post_type as $type ) { | |
// Post in/not in query. | |
if ( isset( $settings->{'posts_' . $type} ) ) { | |
$ids = $settings->{'posts_' . $type}; | |
$arg = 'post__in'; | |
// Set to NOT IN if matching is present and set to 0. | |
if ( isset( $settings->{'posts_' . $type . '_matching'} ) ) { | |
if ( ! $settings->{'posts_' . $type . '_matching'} ) { | |
$arg = 'post__not_in'; | |
} | |
} | |
// Add the args if we have IDs. | |
if ( ! empty( $ids ) && 'post__in' === $arg ) { | |
$post__in = array_merge( $post__in, explode( ',', $ids ) ); | |
} | |
if ( ! empty( $ids ) && 'post__not_in' === $arg ) { | |
$post__not_in = array_merge( $post__not_in, explode( ',', $ids ) ); | |
} | |
} | |
} | |
if ( ! empty( $post__in ) ) { | |
$args['post__in'] = $post__in; | |
} | |
if ( ! empty( $post__not_in ) ) { | |
$args['post__not_in'] = $post__not_in; | |
} | |
return $args; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment