Support

Account

Home Forums Front-end Issues orderby meta_value not working with select field values

Solving

orderby meta_value not working with select field values

  • Need some help here, its driving me nuts

    i have a select field with the following options:
    AbbVie Inc
    Almirall SA
    Amgen Inc.
    AstraZeneca PLC
    Astellas Pharma Inc
    Bayer
    Boehringer Ingelheim GmbH
    Bristol-Myers Squibb Company
    Chugai Pharmaceutical Co Ltd
    Daiichi Sankyo Co Ltd
    Eisai Company Ltd
    Eli Lilly and Company
    Gilead Sciences Inc
    GlaxoSmithKline PLC
    GrĂ¼nenthal GmbH
    F Hoffmann-La Roche Ltd
    H Lundbeck A/S
    Ipsen SAS
    Johnson & Johnson
    Laboratoires Servier
    Merck & Co Inc
    Merck KGaA
    Novartis International AG
    Novo Nordisk A/S
    Pfizer Inc
    Sanofi SA
    Takeda Pharmaceutical Company Ltd
    UCB SA
    Vertex Pharmaceuticals

    in the front end, i need these to display as per the above in alphabetical order. I have tried several soltuions for this, but none seem to actually work.

    below is the code for front end select dropdown:

       <div class="col-md-4">
          <div class="dropdowndiv">
                    <label for ="category">Company:</label>
                    <div class="selectdiv">
                        <select id="company" name="companyfilter">
                        <option value="">All</option>
                    <?php
                    $company = $_GET["personnel_company"];
                     $args = array(
                                'post_type'         => 'personnel',
                                'posts_per_page'    => -1,
                                'order'             => 'ASC',
                                'orderby' => 'meta_value',
                                'suppress_filters' => true,
                                'meta_query'    => array(
                                array(
                                'key'       => 'personnel_company',
                                 'value'     => ''.$company.'',
                                 'compare'   => 'LIKE',
                                )
                                )
                        );
                    $memberquery = new WP_Query($args);
                            while ( $memberquery->have_posts() ) : $memberquery->the_post();
                            if (get_field('personnel_company')) {
                            $company_name = get_field('personnel_company');
                            }
                            if(isset($company_name)  && !empty($company_name)) :
                                if (! in_array( $company_name, $unique_company ) ) :
                                // add to array so it doesn't repeat
                                $unique_company[] = $company_name;     
                            ?>
                        <option value=" <?php echo $company_name ?> " > <?php echo $company_name ?> </option>
                        <?php    
                                endif; 
                            endif;     
                          endwhile;
                        wp_reset_postdata(); ?>
                </select><i class="down-arrow">đŸ¡£</i>
                    </div>
            </div>
        </div>

    output on front end:
    dropdown result

  • image link not working, you can see the front end output here: https://imgur.com/a/OyA2cWO

  • You need to define the meta key to be used to order by, this is different than the meta_query. The easiest way to do this would be to add a ‘meta_key’ => ‘personnel_company’ to your query args (separate from the meta query.)

  • Hi, thanks for the help, but my code was Okay.

    the meta key is in fact there inside the array, and it does work.

    it was in fact the values in the database causing this problem, the data was imported from drupal and was not formatted as text values.

    sorted this out now, but not pleasant!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.