• Resolved amch

    (@amch)


    I have successfully added an extra field following the steps from your guide: https://www.pimwick.com/pw-faq/custom-fields/

    It is visible in the table but I can’t set a filter on this field, which of course was the main point of adding it.

    What do I have to do to set the filter on my custom field?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author pimwick

    (@pimwick)

    Adding custom filters is a little more involved, but it is possible for you to hook into the Bulk Editor to add the filters that you need. Follow these instructions:

    Note: If you are more comfortable with editing your functions.php you can do that instead of Code Snippets.

    1. Download the free Code Snippets plugin: https://www.ads-software.com/plugins/code-snippets/
    2. Create a Snippet with the following code (change where needed):

    function pw_bulk_edit_filter_types( $filter_types ) {
        $filter_types['meta_custom_field'] = array( 'name' => 'Custom field', 'type' => 'text' );
    
        return $filter_types;
    }
    add_filter( 'pwbe_filter_types', 'pw_bulk_edit_filter_types' );
    
    function pw_bulk_edit_common_joins( $common_joins ) {
        global $wpdb;
    
        $common_joins .= "
            LEFT JOIN
                {$wpdb->postmeta} AS meta_custom_field ON (meta_custom_field.post_id = post.ID AND meta_custom_field.meta_key = 'custom_field')
        ";
    
        return $common_joins;
    }
    add_filter( 'pwbe_common_joins', 'pw_bulk_edit_common_joins' );
    
    function pw_bulk_edit_where_clause( $row_sql, $field_name, $filter_type, $field_value, $field_value2, $group_type ) {
    
        if ( 'meta_custom_field' == $field_name ) {
            $sql_builder = new PWBE_SQL_Builder();
            $row_sql = $sql_builder->string_search( 'meta_custom_field.meta_value', $filter_type, $field_value, $field_value2 );
        }
    
        return $row_sql;
    }
    add_filter( 'pwbe_where_clause', 'pw_bulk_edit_where_clause', 10, 6 );
    Thread Starter amch

    (@amch)

    Works really well. Thanks for the great service!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can’t set filter on custom field’ is closed to new replies.