Skip to content

Instantly share code, notes, and snippets.

@chrishow
Created October 15, 2024 05:02
Show Gist options
  • Save chrishow/4ea4d06f018d09f9cdfc1221deb2b4f1 to your computer and use it in GitHub Desktop.
Save chrishow/4ea4d06f018d09f9cdfc1221deb2b4f1 to your computer and use it in GitHub Desktop.
Use ->where('post_title', '=', 'Example Page title') as condition in Carbon Fields
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