Support

Account

Home Forums General Issues Query custom post based on user selections

Solved

Query custom post based on user selections

  • Hi everyone, I could use some assistance here please.

    What I am trying to achieve:
    A salesperson locator dependent on 3 dynamic user select boxes (Country, State, & Industry)

    Here is an example of what I’m looking to do.
    http://mclanahan.com/Contact-Us

    What I have done:
    I already have a custom post type setup to create each salesperson and assign a country, state and industry to each.

    Where I need help
    Creating the front-end select boxes to properly display the correct salesperson. I can build the cascading select boxes with jquery to get the states and industry fields, but I don’t know how to pull the salesperson from the database with the matching criteria.

    Thanks!

  • This will depend on what type of fields you’re using. I’m assuming that you’re using some type of field that’s stored as text, like a text field or a single select field.

    
    $args = array(
      'post_type' => 'salesperson',
      'meta_query' => array(
        'relation' => 'AND',
        array(
          'key' => 'country',
          'value' => $country
        ),
        array(
          'key' => 'state',
          'value' => $state
        ),
        array(
          'key' => 'industry',
          'value' => $industry
        )
      )
    );
    $query = new WP Query($args);
    
  • I’m storing the salesperson name as a text field and all other information as setup as checkboxes on the back end. I’m really new to this plugin and I’m struggling with getting this to work

  • Checkboxes are a bit more difficult to search because the values are stored as serialized arrays. The query would look something like

    
    $args = array(
      'post_type' => 'salesperson',
      'meta_query' => array(
        'relation' => 'AND',
        array(
          'key' => 'country',
          'value' => '"'.$country.'"',
          'compare' = 'LIKE'
        ),
        array(
          'key' => 'state',
          'value' => '"'.$state.'"',
          'compare' = 'LIKE'
        ),
        array(
          'key' => 'industry',
          'value' => '"'.$industry.'"',
          'compare' = 'LIKE'
        )
      )
    );
    $query = new WP Query($args);
    

    there is more information here http://www.advancedcustomfields.com/resources/checkbox/

  • Thanks John, I got the array working with the query and verified by replacing the variable with set values. It did in fact return the correct salesperson. But now I’m on to my other question which was how to pass the variable from basic select boxes in a form to the query and output the results. Below is the working query.

    <?php
    // WP_Query arguments
    $args = array(
      'post_type' => 'sales_locator',
      'meta_query' => array(
        'relation' => 'AND',
        array(
          'key' => 'sl_country',
          'value' => '"'.$country.'"',
          'compare' => 'LIKE'
        ),
        array(
          'key' => 'sl_state',
          'value' => '"'.$state.'"',
          'compare' => 'LIKE'
        ),
        array(
          'key' => 'sl_industry',
          'value' => '"'.$industry.'"',
          'compare' => 'LIKE'
        )
      )
    );
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<h1><?php the_field('sl_name'); ?></h1>
    			<?php the_content(); ?>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    
  • Thanks so much John! I got everything working right. I really appreciate the help.

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

The topic ‘Query custom post based on user selections’ is closed to new replies.