我有一个自定义的帖子类型叫做“bids”。我试图让循环根据meta\\u查询过滤帖子。查询应只允许选中值为“open”的单选按钮的帖子。meta框在admin和save中正确显示,但筛选不起作用。
有人知道我哪里出错了吗?我怀疑它在第二部分的某个地方。。。
/****** CUSTOME POST-TYPE ******/
<?php
// CREATE CUSTOME META BOX
function r_create_mb_bids(){
add_meta_box(
\'r_bid_visibility_mb\',
__( \' Bid Visibility\', \'bids\' ),
\'r_bid_visibility_mb\',
\'bids\',
\'normal\',
\'high\'
);
}
add_action( \'add_meta_boxes\', \'r_create_mb_bids\' );
// DISPLAY IN ADMIN
function r_bid_visibility_mb( $post ){
$bid_data = get_post_meta( $post->ID, \'bid_data\', true );
if( empty($bid_data) ){
$bid_data = array(
\'bid_vis\' => \'\',
);
}
?>
<div class=\'meta meta__group\'>
<label class=\'meta__title\'>Bid Visibility</label>
<input type=\'radio\' name=\'r_inputVis\' value=\'open\' <?php checked( $bid_data["bid_vis"], \'open\' ); ?> /> Open
<input type=\'radio\' name=\'r_inputVis\' value=\'secure\' <?php checked( $bid_data["bid_vis"], \'secure\' ); ?> /> Secure
</div>
<?php
}
add_action( \'save_post_bids\', \'r_save_post_admin_bids\', 10, 3 );
// SAVE META DATA
function r_save_post_admin_bids( $post_id, $post, $update ){
if( !$update ){
return;
}
$bid_data = array();
$bid_data[\'bid_vis\'] = sanitize_text_field( $_POST[ \'r_inputVis\' ]);
update_post_meta( $post_id, \'bid_data\', $bid_data );
}
?>
/****** PAGE TEMPLATE ******/
<?php
// THE LOOP
$args = array(
\'post_type\' => \'bids\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
array(
\'key\' => \'r_inputVis\',
\'value\' => \'open\'
),
),
);
$query = new WP_Query( $args ); while ( $query->have_posts() ) : $query->the_post();
?>
[ ... ]
<?php endwhile; ?>
最合适的回答,由SO网友:Emily Childers 整理而成
我想我找到了另一种方法。看起来效果很好。
/****** CUSTOME POST-TYPE ******/
<?php
// CREATE CUSTOME META BOX
function r_create_mb_bids(){
add_meta_box(
\'r_bid_visibility_mb\',
__( \' Bid Visibility\', \'bids\' ),
\'r_bid_visibility_mb\',
\'bids\',
\'normal\',
\'high\'
);
}
add_action( \'add_meta_boxes\', \'r_create_mb_bids\' );
// DISPLAY IN ADMIN
function r_bid_visibility_mb( $post ){
global $post;
$bid_data = get_post_custom($post->ID);
$visibility = $bid_data[\'visibility\'][0];
?>
<?php $visibility_value = get_post_meta($post->ID, \'visibility\', true);
$visibility_checked = \'checked="checked"\';
?>
<input type=\'radio\' name=\'visibility\' value=\'yes\' <?php if($visibility_value ==\'yes\'){ echo $visibility_checked; } ?>/> Open
<input type=\'radio\' name=\'visibility\' value=\'no\' <?php if($visibility_value ==\'no\'){ echo $visibility_checked; } ?>/> Secure
<?php
}
// SAVE META DATA
add_action(\'save_post\', \'save_details_visibility\');
function save_details_visibility(){
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, \'visibility\', $_POST[\'visibility\']);
}
?>
/****** PAGE TEMPLATE ******/
<?php
// THE LOOP
$args = array(
\'post_type\' => \'bids\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
array ( \'key\' => \'visibility\', \'value\' => \'yes\', )
)
);
$query = new WP_Query( $args ); while ( $query->have_posts() ) : $query->the_post();
?>
[ ... ]
<?php endwhile; ?>
?>