0

I am developing a travel website for an agency. I'm trying to to make the user able to select an option like "Select your departure" and filter the loop with all the departures available for the chosen option. Ie: if I want to check all the departures from Chicago, so the loop will only show the Chicago departures!

<?php
  $args = array(
  'post_type'     => 'travel_packs',
  'order'         => 'ASC',
  );
  $the_query = new WP_Query( $args );

  if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>

//The loop:
<a href="<?php the_permalink(); ?>">
  Destination: <?php the_field('destination'); ?>
  Departing from:<?php the_field('departures'); ?>
  Price:<?php the_field('price'); ?>
</a>

<?php endwhile; endif; ?>

I thought about the $_GET method, but I'm not sure how to assimilate it with the the_field('departures');

Thank you!

0

Now with the new information I would go with this:

?>
<form method="post" action="where_ever_you_want_to_point_the_user_afterwards.php">
/* The loop: */
  if (the_field('departures') == 'user_departure_location') {
   <input type="radio" name="destination" value="<?php echo the_field('destination'); ?>">
   <a href="<?php the_permalink(); ?>">
     Destination: <?php the_field('destination'); ?>
     Departing from:<?php the_field('departures'); ?>
     Price:<?php the_field('price'); ?>
   </a>
  }

/* The loop end */
?></form>

And then on where_ever_you_want_to_point_the_user_afterwards.php you can check which selection he made by doing the following or something, which you prefer.

switch ($_POST["actual_name_of_destination_in_the_form"]) {
  case 'Dallas':
   /* Whatever should happen next */
  break;

  /* Many more cases of course... */
} 

Update: I have found a much nicer way of doing the filter bit... silly me :D

$args = array(
   'post_type' => 'travel_packs',
   'meta_key' => 'departures', 
   'meta_value' => 'USER_DEPARTURES_LOCATION'
   'order' => 'ASC',
);
$query = new WP_Query( $args );

Cheers!

| improve this answer | |
  • Imagine a list of many travel packs to Paris in a website. The site will list all the prices, destination and departures available! However, the departures are mixed in the list, showing departures from Chicago, Orlando, New York, Los Angeles, Houston and so on. I'd like to make the user capable of selecting in a list like "Select your departure". The user could choose "Chicago" and then, the loop will output only the departures available from Chicago! Do you think your answer is the solution for it? Thank you pretty much! – user3301994 Jul 17 '14 at 14:08
  • Now I think I am getting it, you output in the loop the possible travel packages and want the user to select one and then go on with that? – sogathon Jul 17 '14 at 14:18
  • Exactly! So, assume that you live in Chicago and you want to filter that bunch of travel packages to display only the departures available from Chicago! – user3301994 Jul 17 '14 at 14:27
  • 1
    Awesome! I'm just little confused with where the <?php starts and ends! Adding the meta_value made my list filter the departures! Now I'm gonna try this form you coded! I let you know about the result! – user3301994 Jul 17 '14 at 15:15

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