I know there are some questions are similar to my question but mine is a little different I've added a post metadata to all post types called 'admin_bar_status' then created a function that reads all types into a table with a toggle switch to update that metadata to 0 or 1 I did everything well but I have two problems :
when I switch the toggle , it doesn't changed until I pressed once again.
<?php
$post_types = get_post_types();
$i = 1;
?>
<div class="container">
<form id="tableform" method="post">
<table class="table-style" class="right">
<thead>
<th>Post Type</th>
<th>Status</th>
</thead>
<?php
// Loop through each post type
foreach ($post_types as $post_type) {
// Arguments for WP_Query (customize as needed)
$args = array(
'post_type' => $post_type,
'post_status' => 'any',
'posts_per_page' => -1, // Get all posts
);
$the_query = new WP_Query($args);
// Loop through each post
if ($the_query->have_posts()) {
//while ($the_query->have_posts()) {
$the_query->the_post();
// Check if the post has the 'admin_bar_status' metadata
if (metadata_exists('post', get_the_ID(), 'admin_bar_status')) {
// Get the 'admin_bar_status' metadata
$admin_bar_status = get_post_meta(get_the_ID(), 'admin_bar_status', true);
?>
<tr>
<td>
<?php echo $post_type; ?>
</td>
<td align=center>
<label class="toggle-switchy" data-size="sm">
<?php
if ($admin_bar_status == 1) {
?>
<input type="checkbox" id="example2" onchange="submitPostType('<?php echo $post_type; ?>')" checked>
<span class="toggle">
<span class="switch"></span>
</span>
<?php
} else {
?>
<input type="checkbox" id="example2" onchange="submitPostType('<?php echo $post_type; ?>')">
<span class="toggle">
<span class="switch"></span>
</span>
<?php
}
?>
</label>
</td>
</tr>
<?php
}
}
}
?>
</table>
</form>
<script>
function submitPostType(postType) {
var form = document.getElementById('tableform');
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'postType';
input.value = postType;
form.appendChild(input);
form.submit();
}
</script>
</div>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$post_type = $_POST['postType'];
_update_post_type_metadata($post_type);
}
}
also, the main problem is: the function that reads that metadata can't hide the dmin bar for the specific page which has 'admin_bar_status'==0 or hide for all types!
add_action('template_redirect', 'set_admin_bar');
function set_admin_bar()
{
// Get all registered post types
$post_types = get_post_types();
foreach ($post_types as $post_type) {
// Get the admin bar status for the current post type
$args = array(
'post_type' => $post_type,
'post_status' => 'any',
'posts_per_page' => -1, // Get all posts
);
$posts = get_posts($args);
foreach ($posts as $post) {
$admin_bar_status = get_post_meta($post->ID, 'admin_bar_status', true);
// Hide the admin bar if metadata is set to 0
if ($admin_bar_status === '0') {
show_admin_bar(false);
}
}
}
}
please help me
For showing and hiding the admin_bar you need to use filter
add_filter('show_admin_bar', function () {
global $post;
if (is_single() && "0" == get_post_meta($post->ID, 'admin_bar_status', true) {
return false; // hide admin bar
}
return true; // show admin bar
});