vendredi 15 avril 2016

2 Problems with Meta Query on Custom Post Type Archive not returning Matches

I am building a product guide with Wordpress/ACF Pro and I got problems with 2 filters on a custom archive page. I am working with a custom archive template and I am trying to apply different filters by product specifications.

I already made some of them work. I can filter by producer and by price but I have two problems left:

  1. I don't get any matches if the ACF field is a select field where you can select multiple entries
  2. I don't get any matches if the ACF filed is a custom sub field (repeater).

Here's my code from my my_pre_get_posts method in functions.php

add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);

function my_pre_get_posts( $query ) {

    // bail early if is in admin
    if( is_admin() ) {
        return;
    }
    if( !$query->is_main_query() ){
        return;
    }   

    // get meta query
    $meta_query = $query->get('meta_query');

    //  hersteller
    if(!empty($_GET['hersteller'])) {
        $meta_query[] = array(
            'key'       => 'hersteller',
            'value'     => $_GET['hersteller'],
            'compare'   => '=',
        );  
    }   

    //  boardtyp
    if(!empty($_GET['boardtyp'])) {
        $boardtypen = explode(",",$_GET['boardtyp']);

        $meta_query[] = array(
            'key'       => 'boardtyp',
            'value'     => $boardtypen,
            'compare'   => 'IN',
        );  
    }   

    // Preis ab
    if(!empty($_GET['preisVon']) && !empty($_GET['preisBis'])) {
        $preisMin = $_GET['preisVon'];
        $preisMax = $_GET['preisBis'];

        $meta_query[] = array(
            'relation'      => 'AND',
            array(
                'key'       => 'preis_ab',
                'value'     => $preisMin,
                'type'      => 'NUMERIC',
                'compare'   => '>='
            ),
            array(
                'key'       => 'preis_ab',
                'value'     => $preisMax,
                'type'      => 'NUMERIC',
                'compare'   => '<='
            )
        );
    }       

    // Volumen
    if(!empty($_GET['volumenVon']) && !empty($_GET['volumenBis'])) {
        $volumenMin = $_GET['volumenVon'];
        $volumenMax = $_GET['volumenBis'];

        // filter
        function my_posts_where( $where ) { 
            $where = str_replace("meta_key = 'boardgroessen_%", "meta_key LIKE 'boardgroessen_%", $where);

            return $where;
        }
        add_filter('posts_where', 'my_posts_where');        

        $meta_query[] = array(
            'relation'      => 'AND',
            array(
                'key'       => 'boardgroessen_%_volumen1',
                'value'     => $volumenMin,
                'type'      => 'NUMERIC',
                'compare'   => '<='
            ),
            array(
                'key'       => 'boardgroessen_%_volumen1',
                'value'     => $volumenMax,
                'type'      => 'NUMERIC',
                'compare'   => '>='
            )
        );
    }           

    // update meta query
    if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'surfboards' ) {
        var_dump("surfboard query");
        $query->set('meta_query', $meta_query);
    }   

    return; 
}

The first filter "hersteller" (=producer in english) works just fine but the second one "boardtyp" doesn't return any matches. It is exactly the same field with the same attributes, only difference is that for boardtype multiple choices can be selected. In the Docs it says that the value for the meta query has to be an array in this cases and it has to be compared with "IN". Still no matches. I also tried tons of other code alternatives but nothing works. I don't have any idea now where to look for the error or how to debug to find the mistake.

My second problem is to query custom sub fields. In the docs it says that I have to specify the key like I did: parentname_%_childname and that the "where" condition of the SQL statement must be modified. Did both but still no matches.

Filtering by price ("Preis ab") works just fine.

I am pretty sure that the data is correct in the database to filter this stuff. Problem must be somewhere with the query in my opinion but I don't know where. Hope anybody can help and that I provided enough information on this topic.

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire