0

I am trying to gather specific information in a select tag in order to filter pages. A have several training pages with same prices (for instance : $80, $120, $500, etc...). The problem is when a query the custom field 'price_1' (the varible price), wp_query retrieves all the pages with prices, and most of them have the same price.

so my select tag looks like :

Select a price : $80 $80 $80 ... $120 $120 $120 $120 $120 $500 $500 $500 $500 $500 ...

I would like to gather same prices values in one and when click on one of them display all the training pages releated.

Select a price : $80 $120 $500 ...

Thanks for your help if you have some ideas, at this point code is getting tricky.

P.S : Im using ACF plugin

Here is my code :

$args = array(
          'post_type'=> 'page',
          'posts_per_page' => -1,
          'meta_key' => 'prix_1',

        );

$price_query = new WP_Query( $args ); 


<div class="col-lg-3">
   <select name="page-dropdown"> 

    <?php if( $price_query->have_posts() ) : while( $price_query->have_posts() ) : $price_query->the_post(); ?>


        <option value=""><?php the_field('prix_1'); ?></option>


    <?php endwhile; ?>
    <?php endif; ?>

    </select>
</div>
0

With the while loop you can add all of the values to an array ($options). Then, while looping through values you can check to see if the same value appears in the array(in_array($needle,$haystack)). If it does, do not add it to the options array.

Then, loop each of your clean values into the select. As I mention in the code, you may want to wrap the loop into a function and cache it, then return the values when you call the function.

This is untested, but should get you there:

<?php

$args = array(
    'post_type'      => 'page',
    'posts_per_page' => '-1',
    'meta_key'       => 'prix_1',
);

$price_query = new WP_Query( $args );
$options     = array();

//Instead of leaving this inside the page, should wrap into a function inside functions.php
if ( $price_query->have_posts() ) {
    while ( $price_query->have_posts() ) {
        $price_query->the_post();
        $field = get_field( 'prix_1' );
        if ( ! in_array( $field, $options ) ) {
            $options[] = $field; //add to the array
        }
    } //endwhile
}//endif

?>

<div class="col-lg-3">
    <select name="page-dropdown">

        <?php if ( ! empty( $options ) ) { //this should no longer have duplicates
            foreach ( $options as $option ) { ?>
                <option value=""><?php echo $option; ?></option>
            <?php } //end foreach
        } //endif ?>
    </select>
</div>
  • @Nick83 Happy to hear it! Don't forget to mark the answer as well. – Tom Oct 18 '17 at 13:36
  • Hi Tom, could you suggest what needs to be done in order to display pages from each price value when selected ? Ex : if I select $80 -> displays all pages witch have this price inside. I've tryied different things but not working (array_values, $option->prix_1). Thank you for your help ! – Nick83 Oct 18 '17 at 20:50
  • @Nick83 A few questions: 1. Do you want to do this without the page refreshing? 2. Do you plan to put this in multiple locations? It may be better to open a new question and link it here so I can give a more detailed answer than a comment. – Tom Oct 19 '17 at 0:26
  • 1. I would like to display results in the current page by the easiest way. 2.The code is in a filter.php, included in 4 category pages ( if you want have quick look : bit.ly/2gmWrmh). Here is the new question link : bit.ly/2ytxO1O //Thanks again – Nick83 Oct 19 '17 at 12:33
  • @Nick83 I submitted an answer to your other question – Tom Oct 23 '17 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.