Created
October 15, 2024 05:02
-
-
Save chrishow/4ea4d06f018d09f9cdfc1221deb2b4f1 to your computer and use it in GitHub Desktop.
Use ->where('post_title', '=', 'Example Page title') as condition in Carbon Fields
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
class Post_Title_Condition extends Carbon_Fields\Container\Condition\Condition | |
{ | |
/** | |
* Check if the condition is fulfilled | |
* | |
* @param array $environment | |
* @return bool | |
*/ | |
public function is_fulfilled($environment) | |
{ | |
// var_dump($environment); | |
$post = $environment['post'] ?? null; | |
$result = $this->compare( | |
$post->post_title ?? false, | |
$this->get_comparison_operator(), | |
$this->get_value() | |
); | |
// var_dump($result); | |
return $result; | |
} | |
} | |
\Carbon_Fields\Carbon_Fields::extend(Post_Title_Condition::class, function ($container) { | |
// create an instance of our new class | |
$condition = new Post_Title_Condition(); | |
$condition->set_comparers(\Carbon_Fields\Carbon_Fields::resolve('generic', 'container_condition_comparer_collections')); | |
return $condition; | |
}); | |
add_filter('carbon_fields_post_meta_container_static_condition_types', function ($condition_types, $container_type, $container) { | |
return array_merge( | |
$condition_types, | |
array('post_title') | |
); | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment