I have a custom post type called "bids". I'm trying to get the loop to filter posts according to a meta_query. The query should allow for only posts that have the radio button with the value "open" checked. The meta boxes show up properly in the admin and save, but the filtering is not working.
Anyone know where I went wrong? I suspect it's somewhere in the 2nd section...
/****** 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; ?>
I have a custom post type called "bids". I'm trying to get the loop to filter posts according to a meta_query. The query should allow for only posts that have the radio button with the value "open" checked. The meta boxes show up properly in the admin and save, but the filtering is not working.
Anyone know where I went wrong? I suspect it's somewhere in the 2nd section...
/****** 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; ?>
I think I found another way. Seems to be working great.
/****** 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; ?>
?>
echo $query->request;
just before the white statement? That will help to see what SQL is actually running. Secondly, you could check thewp_postmeta
table to see ifr_inputVis
is there. – Alexander Holsgrove Commented Feb 8, 2019 at 23:49define('WP_DEBUG', true);
set, and do you see anything in your site error log file? – Alexander Holsgrove Commented Feb 9, 2019 at 0:38