Meta Query Filtering not working on Custom Meta Box using Radio Buttons

admin2025-06-03  4

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; ?>
Share Improve this question asked Feb 8, 2019 at 23:14 Emily ChildersEmily Childers 231 silver badge3 bronze badges 5
  • Can you please add echo $query->request; just before the white statement? That will help to see what SQL is actually running. Secondly, you could check the wp_postmeta table to see if r_inputVis is there. – Alexander Holsgrove Commented Feb 8, 2019 at 23:49
  • hmmmm. nothing comes up with the query request... – Emily Childers Commented Feb 9, 2019 at 0:26
  • if I remove the meta_query section and just sort by 'bids' and posts_per_page, I get this: SELECT eh_posts.* FROM eh_posts WHERE 1=1 AND eh_posts.post_type = 'bids' AND (eh_posts.post_status = 'publish') ORDER BY eh_posts.post_date DESC – Emily Childers Commented Feb 9, 2019 at 0:27
  • That's odd you get the request SQL by removing the meta query. Can you try a different meta query with another field and see if that works? Do you have define('WP_DEBUG', true); set, and do you see anything in your site error log file? – Alexander Holsgrove Commented Feb 9, 2019 at 0:38
  • I set WP_DEBUG to true and ended up trying another method. Seems to be working great. Thanks for all your help. – Emily Childers Commented Feb 11, 2019 at 20:37
Add a comment  | 

1 Answer 1

Reset to default 0

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; ?>


?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748917861a314803.html

最新回复(0)