Custom Fields
Re-Envisioned
Support MB Custom Table search by custom field in MB custom table

  • Creator
    Topic
  • #19190
    Resolved Brian
    Participant

    Hi Long,
    I’m following along with this guide for creating a search by custom field and displaying results: https://metabox.io/get-posts-by-custom-fields-in-wordpress/

    How can I do this if my fields are in a MB custom table. What would I need to amend to your guide. I only need to search by 1 filed. Then once it finds the correct post would need to display a few custom field values.

    I’m reading the MB custom table docs at bottom when it talks about query post. But I wasn’t sure what to add in the sql query in your example: field1 = ‘value1’. Isn’t my value the metabox filed value? Would I do something like: $valuename = rwmb_meta( ‘field1_name’ );
    field1_name = ‘$valuename’ and field2 = ‘$value2name’ for that section?

    Trying to follow along with the first doc to adapt for MB custom table but just a little lost.

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #19197
    Long Nguyen
    Moderator

    Hi Brian,

    If you want to create a search by custom field and display results (posts), follow this documentation, field1 is the ID of the field or column key in the custom table, in the example of the document it would be address or phone or email and the value1 is the input value of search form.

    The code gets post by the custom field which is stored in the custom table should be

    global $wpdb;
    $ids = $wpdb->get_col( "SELECT ID FROM my_custom_table WHERE address = 'input-search-value'" );
    
    if ( empty( $ids ) ) {
        echo 'There is no posts';
    } else {
        $query = new WP_Query( [
            'post_type' => 'post',
            'post__in'  => $ids,
        ] );
        // Do something
        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a><br>';
    
        }
        wp_reset_postdata();
    }
    #19229
    Brian
    Participant

    Hi Long,
    That worked – thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.