所以我在一个组中有 5 个 acf 字段,其中 3 个是复选框,其余是文本,只有 3 个下拉菜单显示,所有 3 个显示帖子具有的值,但实际上只有 2 个会在选择过滤器帖子时显示。 这是代码:
function showww() {
// Get all ACF fields for the post type
$fields = get_field_objects();
// Check if form is submitted
if (isset($_POST['submit'])) {
// Loop through all fields
foreach ($fields as $name => $field) {
// Get field value from form input
$value = isset($_POST[$name]) ? $_POST[$name] : null;
// If the value is an array, serialize it
if (is_array($value)) {
$value = serialize($value);
}
// Add filter for selected field value
if ($value) {
add_filter('posts_where', function($where) use ($field, $value) {
global $wpdb;
// If the value is an array, unserialize it and convert it to a string
if (is_serialized($value)) {
$value = implode(',', unserialize($value));
}
$where .= " AND EXISTS (
SELECT * FROM $wpdb->postmeta
WHERE post_id = $wpdb->posts.ID
AND meta_key = '{$field['name']}'
AND meta_value = '{$value}'
)";
return $where;
});
}
}
}
// Retrieve posts based on filters
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'suppress_filters' => false, // Make sure filters are applied
);
$posts = get_posts($args);
// Remove filters
foreach ($fields as $name => $field) {
remove_filter('posts_where', function($where) use ($field, $value) {
global $wpdb;
// If the value is an array, unserialize it and convert it to a string
if (is_serialized($value)) {
$value = implode(',', unserialize($value));
}
$where .= " AND EXISTS (
SELECT * FROM $wpdb->postmeta
WHERE post_id = $wpdb->posts.ID
AND meta_key = '{$field['name']}'
AND meta_value = '{$value}'
)";
return $where;
});
}
// Display form
echo '<form method="post">';
foreach ($fields as $name => $field) {
echo '<label for="' . $name . '">' . $field['label'] . '</label><br>';
echo '<select id="' . $name . '" name="' . $name . '">';
echo '<option value="">All</option>';
// Get all distinct field values for the post type
$values = get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => $field['name'],
'fields' => 'ids',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => $field['name'],
'compare' => 'EXISTS',
),
),
));
$values = array_unique(array_map(function($id) use ($field) {
$value = get_post_meta($id, $field['name'], true);
// If the value is an array, serialize it
if (is_array($value)) {
$value = serialize($value);
}
return $value;
}, $values));
if (count($values) === 1 && is_array(unserialize($values[0]))) {
// If the field value is an array with only one option, select that option by default
$selected = unserialize($values[0])[0];
} else {
$selected = isset($_POST[$name]) ? $_POST[$name] : null;
}
foreach ($values as $value) {
// If the value is an array, unserialize it and convert it to a string
if (is_serialized($value)) {
$value = implode(',', unserialize($value));
}
echo '<option value="' . $value . '"';
if ($value === $selected) {
echo ' selected';
}
echo '>' . $value . '</option>';
}
echo '</select><br>';
}
echo '<input type="submit" name="submit" value="Search">';
echo '</form>';
// Display list of posts
if ($posts) {
echo '<ul>';
foreach ($posts as $post) {
echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
}
echo '</ul>';
} else {
echo 'No posts found';
}
}
add_shortcode( 'showww', 'showww' );
我尝试显示复选框的值与那些只是文本值的值不同它仍然没有显示所有字段并且过滤器不起作用