logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$6
Sort Posts By Custom Meta On A Page

Hi There

I have Properties listed on a Page. This is all fine however these property posts I wish to be sortable onclick by Price, Bedrooms and Date

I know I am in the right area but struggling at the last to get this sorted ....


<?php
$by_rooms= esc_url(add_query_arg(array('meta_key'=>'_leaf_no_of_bedrooms','orderby'=>'meta_value_num')));
$by_price = esc_url(add_query_arg(array('meta_key'=>'_leaf_property_cost','orderby'=>'meta_value_num')));
$by_date = esc_url(add_query_arg(array('meta_key'=>false,'orderby'=>'date')));
?>

<ul>
<li> <a href="<?php echo $by_price;?>">Order by price</a></li>
<li> <a href="<?php echo $by_rooms;?>">Order by rooms</a></li>
<li> <a href="<?php echo $by_date;?>">Order by date</a></li>
</ul>


The correct strings are being applied to the URL but the posts arent being sorted.

I am thinking that it is something to do with the fact this is being called on a page but I do not know how to set get_query_var etc etc

Hope this is clear and look forward to teh help

John

This question has been answered.

kiddamedia | 06/12/12 at 9:46am Edit

Previous versions of this question: 06/12/12 at 2:14pm

The experts have suggested, on average, a prize of $20 for this question.

(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/12/12
    10:36am
    Just Me says:

    Did you try to add order=ASC (or DESC)?

    • 06/12/12 10:49am

      kiddamedia says:

      Yes that is not the issue, I already have 'orderby'=>'meta_value_num' set.

      The issue is to do with being on a Page not say an archive etc and I do not know how to call the parametters from get_query_var which I think is the issue and need help on

    • 06/12/12 10:56am

      Just Me says:

      do you have a link?

    • 06/12/12 11:20am

      Just Me says:

      http://codex.wordpress.org/Function_Reference/query_posts

      This might help

      global $query_string;
      query_posts( $query_string . '&orderby=' );

  • avatar
    Last edited:
    06/12/12
    11:21am
    Francisco Javier Carazo Gil says:

    Hi,

    I recommend you use directly a new query with query_posts: http://codex.wordpress.org/Function_Reference/query_posts

    Then you can do ordering and whatever you want, i. e.:

    query_posts( array( 'category__and' => array(1,3), 'posts_per_page' => 2, 'orderby' => 'title', 'order' => 'DESC' ) );

    • 06/12/12 11:41am

      kiddamedia says:

      I think you slightly misunderstand what I am attemtping to do

      The point is not sort the posts a particular way based on initial query, that is already done.

      The unordered list has links that upon click reorder the posts, using add_query_arg

      The string is correctly being appended to the URL however posts are not reordering and I think it is because I am on a Page.

  • avatar
    Last edited:
    06/12/12
    12:43pm
    Arnav Joy says:

    try adding post_type => post as follows:-

    add_query_arg(array('meta_key'=>'_leaf_no_of_bedrooms','orderby'=>'meta_value_num' , 'post_type' => 'post'))

    • 06/12/12 1:24pm

      Arnav Joy says:

      try this

      $args = array(

      'order' => 'ASC',

      'post_type' => 'leaf_properties',

      'meta_key'=> get_query_var('meta_key'),

      'orderby' => get_query_var('orderby')



      );

    • 06/12/12 1:39pm

      kiddamedia says:

      Thanks Arnav but sadly that didn't work.

  • avatar
    Last edited:
    06/12/12
    12:44pm
    Gabriel Reguly says:

    Hi kiddamedia,

    What is the code of your page? Can you post it here?

    I suspect your properties are custom post types (CPTs) and your page has a query for them.

    Regards,
    Gabriel

    • 06/12/12 12:48pm

      kiddamedia says:

      Yes you are correct, properties is a CPT and I have a loop to display them on my page.

      Apologies, I should have posted teh full page code first but here go's ....


      <?php get_header(); ?>
      <?php get_sidebar('left'); ?>

      <div class="main-other left">
      <h3>
      <?php the_title(); ?>
      </h3>
      <?php
      $by_rooms= esc_url(add_query_arg(array('meta_key'=>'_leaf_no_of_bedrooms','orderby'=>'meta_value_num')));
      $by_price = esc_url(add_query_arg(array('meta_key'=>'_leaf_property_cost','orderby'=>'meta_value_num')));
      $by_date = esc_url(add_query_arg(array('meta_key'=>false,'orderby'=>'date')));
      ?>

      <ul>
      <li> <a href="<?php echo $by_price;?>">Order by price</a></li>
      <li> <a href="<?php echo $by_rooms;?>">Order by rooms</a></li>
      <li> <a href="<?php echo $by_date;?>">Order by date</a></li>
      </ul>


      <?php

      $args = array(
      'order' => 'ASC',
      'post_type' => 'leaf_properties',
      );

      $query = new WP_Query( $args );

      // The Loop
      while ( $query->have_posts() ) : $query->the_post();
      $terms = get_the_terms( $post_id, 'property_type' );
      foreach ($terms as $term);
      echo '<div class="property-box cf"><a href="'.get_permalink().'">' .get_the_post_thumbnail($post->ID, 'property-overview-thumb', $img_attr). '</a><p><span>&pound;'.get_post_meta( $post->ID, '_leaf_property_cost', true ).'&nbsp;' .get_post_meta( $post->ID, '_leaf_cost_duration', true ).'</span></p><p>'.get_post_meta( $post->ID, '_leaf_no_of_bedrooms', true ). ' bedroom ' . $term->name . ' to rent</p><p>' .get_the_title(). '</p><p>Added To Site ' .get_the_date('F j, Y'). '</p><p>' .get_the_excerpt(). '</p></div>';
      endwhile;

      // Reset Post Data
      wp_reset_postdata();
      ?>
      </div>
      <?php get_footer(); ?>

    • 06/12/12 1:12pm

      Gabriel Reguly says:

      Hi,

      So here is where you should amend your code:


      $args = array(
      'order' => 'ASC',
      'post_type' => 'leaf_properties',
      );


      Regards,
      Gabriel

    • 06/12/12 1:17pm

      kiddamedia says:

      Amend to what?

    • 06/12/12 1:45pm

      Gabriel Reguly says:

      Hi,

      This is the full amended code


      <?php get_header(); ?>
      <?php get_sidebar('left'); ?>
      <div class="main-other left">
      <h3><?php the_title(); ?></h3>
      <?php
      $by_rooms= esc_url(add_query_arg(array('orderby'=>'rooms')));
      $by_price = esc_url(add_query_arg(array('orderby'=>'price')));
      $by_date = esc_url(add_query_arg(array('orderby'=>'date')));
      ?>
      <ul>
      <li> <a href="<?php echo $by_price;?>">Order by price</a></li>
      <li> <a href="<?php echo $by_rooms;?>">Order by rooms</a></li>
      <li> <a href="<?php echo $by_date;?>">Order by date</a></li>
      </ul>
      <?php
      if ( ! isset( $wp_query->query['orderby'] ) ) {
      $args = array(
      'order' => 'ASC',
      'post_type' => 'leaf_properties'
      );
      } else {
      switch ($wp_query->query['orderby']) {
      case 'date':
      $args = array(
      'orderby' => 'date',
      'post_type' => 'leaf_properties'
      );
      break;
      case 'price':
      $args = array(
      'meta_key'=>'_leaf_property_cost',
      'orderby' => 'meta_value_num',
      'post_type' => 'leaf_properties'
      );
      break;
      case 'rooms':
      $args = array(
      'meta_key'=>'_leaf_no_of_bedrooms',
      'orderby' => 'meta_value_num',
      'post_type' => 'leaf_properties'
      );
      break;
      }
      }
      $query = new WP_Query( $args );
      // The Loop
      while ( $query->have_posts() ) : $query->the_post();
      $terms = get_the_terms( $post_id, 'property_type' );
      foreach ($terms as $term);
      echo '
      <div class="property-box cf">
      <a href="'.get_permalink().'">' .get_the_post_thumbnail($post->ID, 'property-overview-thumb', $img_attr). '</a>
      <p><span>&pound;'.get_post_meta( $post->ID, '_leaf_property_cost', true ).'&nbsp;' .get_post_meta( $post->ID, '_leaf_cost_duration', true ).'</span></p>
      <p>'.get_post_meta( $post->ID, '_leaf_no_of_bedrooms', true ). ' bedroom ' . $term->name . ' to rent</p>
      <p>' .get_the_title(). '</p>
      <p>Added To Site ' .get_the_date('F j, Y'). '</p>
      <p>' .get_the_excerpt(). '</p>
      </div>';
      endwhile;
      // Reset Post Data
      wp_reset_postdata();
      ?>
      </div>
      <?php get_footer(); ?>


      If you could raise the prize, I would appreciate it.

      Regards,
      Gabriel

This question has expired.



Francisco Javier Carazo Gil, Arnav Joy, kiddamedia voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.