Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Sort Posts By Custom Meta On A Page WordPress

  • SOLVED

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

Answers (4)

2012-06-12

Gabriel Reguly answers:

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


kiddamedia comments:

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(); ?>


Gabriel Reguly comments:

Hi,

So here is where you should amend your code:


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


Regards,
Gabriel


kiddamedia comments:

Amend to what?


Gabriel Reguly comments:

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

2012-06-12

Just Me answers:

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


kiddamedia comments:

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


Just Me comments:

do you have a link?


Just Me comments:

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

This might help

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

2012-06-12

Francisco Javier Carazo Gil answers:

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' ) );


kiddamedia comments:

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.

2012-06-12

Arnav Joy answers:

try adding post_type => post as follows:-

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


Arnav Joy comments:

try this

$args = array(

'order' => 'ASC',

'post_type' => 'leaf_properties',

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

'orderby' => get_query_var('orderby')



);


kiddamedia comments:

Thanks Arnav but sadly that didn't work.