I have created a Custom filter for a custom post. I based my code from here - Add filter menu to admin list of posts (of custom type) to filter posts by custom field values and it works just fine.

However, it only lists the titles of the post upon filtering. How can I include the meta values as return values so I can show it beside the post (columns)? Thanks.

Update (here is my code):

function wpse45436_posts_filter( $query ){

    global $pagenow;
    $type = 'post';

    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    }
    if ( ('custom_post_1' == $type || 'custom_post_2' == $type || 'custom_post_3' == $type) && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {

        global $wpdb;
        $search_query = $_GET['ADMIN_FILTER_FIELD_VALUE'];

        $custom_key_1 = "custom_key_1";
        $custom_result_1 = $wpdb->get_results( "SELECT * FROM wp_postmeta WHERE meta_key = '$custom_key_1' AND meta_value = '$search_query'");

        $custom_id_1 = array(); 
        if ($custom_result_1) {
            foreach ($custom_result_1 as $value) {
                $custom_id_1[] = $value->post_id;
            }
        }

        $query_result = $wpdb->get_results( "SELECT * FROM wp_posts WHERE post_type = '$type'");


        $custom_id_2 = array(); 
        if ($query_result) {
            foreach ($query_result as $value) {
                $custom_id_2[] = $value->ID;
            }
        }


        $custom_id_3 = array();

        foreach ($custom_id_2 as $id) {
            $meta_value = get_post_meta( $id, 'custom_value_1' );
            foreach ($meta_value as $key => $value) {
                if (in_array($value, $custom_id_1)) {   
                    $custom_id_3[] = $id;
                }   
            }
        }

        $meta_query = array(
            'relation' => 'OR',
            array(
                'key' => "custom_key_2",
                'value' => $_GET['ADMIN_FILTER_FIELD_VALUE'],
                'compare' => '='
            ),
            array(
                'key' => "custom_key_3",
                'value' => $_GET['ADMIN_FILTER_FIELD_VALUE'],
                'compare' => '='
            ),  
        );

        if (!empty($custom_id_3)){
            $query->query_vars['post__in'] = $custom_id_3;
        }
        else{
            $query->query_vars['meta_query'] = $meta_query;
        }
    }
}
add_filter( 'parse_query', 'wpse45436_posts_filter' );


  • Show your code, pls. It'll be a little bit hard to help without seeing it... – Krzysiek Dróżdż Jun 27 at 12:43
  • Please show what you've already tried and you're more likely to get useful answers. – Andy Macaulay-Brook Jun 27 at 21:06

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.