I have a custom post type setup using metabox with a custom field using a simple checkbox. The goal is that each department in an organization can check and uncheck sections that they want displayed on their department’s page.
I’m attempting to use the conditions builder api along with custom written php to get the right output.
I’m having the php output the custom post type ID based on the page’s taxonomy to look up the display_staff custom field for that post type. Then loop that into the Bricks condition builder.
Am I making this more complicated than it needs to be? Here’s the code I’ve been working with:
<?php
// Conditions Builder API
// Custom condition group
add_filter("bricks/conditions/groups", "add_my_custom_condition_group");
function add_my_custom_condition_group($groups)
{
$groups[] = [
"name" => "custom_department_conditions",
"label" => esc_html__("Department Conditions", "text-domain"),
];
return $groups;
}
// Custom condition options to the group
add_filter("bricks/conditions/options", "add_my_custom_conditions_to_group");
function add_my_custom_conditions_to_group($options)
{
$options[] = [
"key" => "department_display_staff",
"label" => esc_html__("Display Staff", "text-domain"),
"group" => "custom_department_conditions",
"compare" => [
"type" => "select",
"options" => [
"==" => esc_html__("is", "text-domain"),
"!=" => esc_html__("is not", "text-domain"),
],
],
"value" => [
"type" => "text",
],
];
return $options;
}
// Function to store and retrieve department data
function get_department_data()
{
// Check if data is already cached
$cached = get_transient("department_data");
if ($cached) {
return $cached;
}
// Initialize an empty array to store the data
$department_data = [];
// Retrieve all posts with the 'department' taxonomy
$args = [
"post_type" => "department",
"posts_per_page" => -1,
"tax_query" => [
[
"taxonomy" => "department",
"field" => "id",
"terms" => get_terms([
"taxonomy" => "department",
"fields" => "ids",
]),
],
],
];
$posts_query = new WP_Query($args);
// Check if any posts are found
if ($posts_query->have_posts()) {
while ($posts_query->have_posts()) {
$posts_query->the_post();
$post_id = get_the_ID();
$terms = get_the_terms($post_id, "department");
// Initialize an array to store post data
$post_data = ["post_id" => $post_id, "taxonomy" => []];
// Collect taxonomy names
if ($terms && !is_wp_error($terms)) {
foreach ($terms as $term) {
$post_data["taxonomy"][] = $term->name;
}
}
// Find pages with the same taxonomy
$page_args = [
"post_type" => "page",
"posts_per_page" => -1,
"tax_query" => [
[
"taxonomy" => "department",
"field" => "id",
"terms" => wp_list_pluck($terms, "term_id"),
],
],
];
$pages_query = new WP_Query($page_args);
// Check if any pages are found
if ($pages_query->have_posts()) {
$post_data["associated_pages"] = [];
while ($pages_query->have_posts()) {
$pages_query->the_post();
$page_id = get_the_ID();
// Store associated page IDs in the array
$post_data["associated_pages"][] = $page_id;
}
wp_reset_postdata(); // Reset the page data
}
// Push the post data to the department data array
$department_data[] = $post_data;
}
wp_reset_postdata(); // Reset the post data
} else {
// If no posts are found, set department data to empty array
$department_data = [];
}
// Cache the department data for 12 hours to optimize performance
set_transient("department_data", $department_data, 12 * HOUR_IN_SECONDS);
return $department_data;
}
// Boolean result for conditions
add_filter(
"bricks/conditions/result",
"check_display_staff_for_current_page",
10,
3
);
function check_display_staff_for_current_page(
$result,
$condition_key,
$condition
) {
if ($condition_key !== "department_display_staff") {
return $result;
}
// Get the stored department data
$department_data = get_department_data();
// Get the current post ID
$current_post_id = get_the_ID();
// Loop through department data to find the matching department
foreach ($department_data as $department) {
if ($department["post_id"] == $current_post_id) {
// Check if 'Display Staff' is set to true for this department
if (in_array("true", $department["taxonomy"])) {
return true;
}
break;
}
}
return false;
}