Support

Account

Home Forums General Issues Problem with checkbox using custom field archive

Unread

Problem with checkbox using custom field archive

  • How can i get archive base a custom field.

    I try this but my hosting say there is a problem with my code, the code make manny select in the database..

    I have 1 post type, and 2 custom field

    Post Type : produse
    custom field : culoare
    custom field : marime

    http://prntscr.com/34nn03

    I am using Version 4.3.5
    How can i make a corect and not loading problem script… Can somebody tell me were i do rong?

    This i have in function.php

    /* ------------ Search Avansat -----------*/
    
    add_action('pre_get_posts', 'my_pre_get_posts');
    
    function my_pre_get_posts( $query ) {
        if( is_admin() ) { return; }
        $meta_query = $query->get('meta_query'); // get original meta query
    
        // validate type
        if( (empty($_GET['culoare'])) && (empty($_GET['marime']))  )
        {
            return;
        }
    
        // get types as an array
        // - use explode to get an array of values from type=a|b|c
        $culori = explode('|', $_GET['culoare']);
        $marimi = explode('|', $_GET['marime']);
    
        // set compare for the meta_query
        // - http://codex.wordpress.org/Class_Reference/WP_Query
        $meta_query['relation'] = 'OR';
    
        foreach( $culori as $culoare )
        {
            $meta_query[] = array(
                'key'       => 'culoare',
                'value'     => '"' . $culoare . '"',
                'compare'   => 'LIKE',
            );
        }
    
        foreach( $marimi as $marime )
        {
            $meta_query[] = array(
                'key'       => 'marime',
                'value'     => '"' . $marime . '"',
                'compare'   => 'LIKE',
            );
        }
    
        $query->set('meta_query', $meta_query); // update the meta query args
    
        return; // always return
    }
    

    in my php arhive file i have this

    <?php 
    $field = get_field_object('culoare');
    $values = explode(',', $_GET['culoare']);
    ?>
    <div id="search-culoare">
    <form>
      <fieldset>
        <legend>Cauta dupa culoare:</legend>
            <?php if (!empty($field['choices'])) : $i = 0; foreach( $field['choices'] as $choice_value => $choice_label ): ?>
            <?php $culoareIndex = strtolower($choice_value); $i ++; ?>
            <input type="checkbox" name="culoare[]" value="<?=$choice_value?>" id="checkbox-1-<?=$i?>" class="regular-checkbox-<?=$culoareIndex?>"
            <?php if( in_array($choice_value, $values) ): ?> checked="checked" <?php endif; ?> />
            <label for="checkbox-1-<?=$i?>"></label>
            <?php endforeach; endif; ?>
    
        </fieldset>
    </form>   
    <script type="text/javascript">
    (function($) { 
        $('#search-culoare').on('change', 'input[type="checkbox"]', function(){
            // vars
            var vals = [];
            $('#search-culoare').find('input:checked').each(function(){
                vals.push( $(this).val() );
            });
            vals = vals.join("|");
            window.location.replace('/produse/?culoare=' + vals);
            console.log( vals );
        });
    })(jQuery); 
    </script>

    The select that my hosting provider say:

    15561572 cinemago_       localhost cinemago_c      1157  Query SELECT post_id, meta_key, meta_value FROM co_postmeta WHERE post_id IN (32,101) ORDER BY meta_id ASC
    
    15561580 cinemago_       localhost cinemago_c      1156  Query SELECT post_id, meta_key, meta_value FROM co_postmeta WHERE post_id IN (32,101) ORDER BY meta_id ASC
    
    15561603 cinemago_       localhost cinemago_c      1155  Query SELECT post_id, meta_key, meta_value FROM co_postmeta WHERE post_id IN (32,101) ORDER BY meta_id ASC

    Thanks in advance!

Viewing 1 post (of 1 total)

The topic ‘Problem with checkbox using custom field archive’ is closed to new replies.