Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You'll have to give us a bit more detail. Are you getting an error? If not, what are you expecting to see, and what are you actually seeing?</p> <p>One possible problem is you're passing "Price High-Low" even if they select "Price Low-High". This line:</p> <pre><code>&lt;option value="Price High-Low" &lt;?php echo ($resultsorder == 'Price High-Low')? 'selected="selected"':''; ?&gt;&gt;Price Low-High&lt;/option&gt; </code></pre> <p>is wrong.</p> <p><strong>Edit:</strong></p> <p>Ok, I've created a simple theme that duplicates what you seem to have. In <code>functions.php</code> I have:</p> <pre><code>&lt;?php add_action('init', 'create_listing_post_type'); function create_listing_post_type() { register_post_type('listing', array( 'labels' =&gt; array( 'name' =&gt; 'Listings', 'singular_name' =&gt; 'Listing', 'add_new_item' =&gt; 'Add a listing', 'edit_item' =&gt; 'Edit listing', 'new_item' =&gt; 'Add a listing', 'search_items' =&gt; 'Find a listing', 'not_found' =&gt; 'No listing found', 'not_found_in_trash' =&gt; 'No listing found in the trash' ), 'public' =&gt; true, 'supports' =&gt; array('title', 'editor', 'custom-fields') )); } ?&gt; </code></pre> <p>I went into the admin screen and added three listings:</p> <ol> <li>Listing A, with an address of "Switzerland" and a price of 10000.</li> <li>Listing B, with an address of "Australia" and a price of 50000.</li> <li>Listing C, with an address of "UK" and a price of 20000.</li> </ol> <p>I then added a new template in <code>showlistings.php</code> based on your code:</p> <pre><code>&lt;?php /** * Template Name: showlistings */ get_header(); //$resultsorder = 'Price High-Low'; //$resultsorder = 'Price Low-High'; $resultsorder = 'a-z'; switch ($resultsorder) { case "Price High-Low": $metakey = 'price'; $order = 'DESC'; $orderby = 'meta_value_num'; break; case "Price Low-High": $metakey = 'price'; $order = 'ASC'; $orderby = 'meta_value_num'; break; case "a-z": $metakey = 'address'; $order = 'ASC'; $orderby = 'meta_value'; break; } $wpq = array ( 'post_type' =&gt;'listing', 'meta_key' =&gt; $metakey, 'orderby' =&gt; $orderby, 'order' =&gt; $order); $listings = new WP_Query($wpq); foreach( $listings-&gt;posts as $listing) { echo $listing-&gt;post_title; echo '&lt;br /&gt;'; } get_footer(); ?&gt; </code></pre> <p>The good news is your code works for me: as I changed the value of the <code>$resultsorder</code> (changing the line commented out), I got the results expected.</p> <p>So that leaves a few things to check:</p> <ol> <li>Is the value of <code>$resultsorder</code> set as you expect (i.e. is it being submitted from your form, and is it being populated from the <code>$_POST</code> variable? put <code>echo "\$resultsorder = $resultsorder";</code> somewhere in your output to check.</li> <li>Are the <code>price</code> values stored in a way MySQL can parse them as numbers? For example, "10000", not (say) "$10,000". WordPress has MySQL convert the numbers to strings by adding 0 to them - if they can't be parsed as numbers, they'll all return 0 and any ordering will be meaningless.</li> <li>Are the <code>$alllistings</code> and <code>$_ids</code> variables populated as you expect (i.e. is the expected branch of your <code>if</code>-<code>then</code> logic being executed? Again, using <code>echo</code> (or <code>print_r</code>) will help you check the values.</li> <li>Do your string literals match exactly? Might be worth verifying that there are no subtle differences between your form values and the values you're checking in your PHP. You could have similar looking but different characters, or subtle case differences (eg "Price High-Low" vs "Price High-low")</li> </ol> <p>I'd break up your investigation. Check that the form submission is working, then that the query arguments are being set correctly, then that the results are as expected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload