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

How to allow sort for custom field value for ASC and DESC? WordPress

  • SOLVED

Hi,

I am using custom field query plugin. I want to create a sort option in the front end of wp to allow user to sort price from lowest to highest or highest to lowest.

I register the function in my custom function of headwaytheme.
add_action('init', 'register_From_Price');
function register_From_Price(){
register_custom_queryable_field("From_Price", array("dataType"=>"numeric","order"=>"DESC"));
}


By changing the default to 'ASC'. Then I place the following code:
<span class="lowest"><a href="<?php echo add_query_arg('order_by','From_Price', 'order'=>'ASC');?>">Lowest</a>
</span>

<span class="highest"><a href="<?php echo add_query_arg( 'order_by', 'From_Price' ); ?>">Highest</a>
</span>

<span class="clearfilter"><a href="<?php echo add_query_arg( 'order_by', ' ' ); ?>">Clear Filter</a>
</span>


It is working for the 'highest' option, mainly I think is default. But for the lowest, it is not working. And If I click on the 'lowest' option, since it is not working, the url will still stay unchanged even though I added a option call 'clear filter', the url will not change back though the post do reorder back to the original state.

Let me know of any suggestion or solution.

Answers (2)

2011-10-29

Luis Abarca answers:


Check this answer too http://wpquestions.com/question/show/id/2204, Julian Lannigan create a plugin for a similar problem.


wan comments:

I have read that, but it does not help me. My register function work well, its the link on the front end that is not working.

echo add_query_arg('order_by','From_Price') just output the default on whatever I have registered.

echo add_query_arg('order_by','From_Price', 'order'=>'ASC') is not working and the sort never take place. Url also unchanged.

It needs to be order_by, orderby do not work.


Luis Abarca comments:

try add_query_arg in this way:


<span class="lowest"><a href="<?php echo add_query_arg( array('order_by' => 'From_Price', 'order' => 'ASC') ) ?>">Lowest</a>
</span>

<span class="highest"><a href="<?php echo add_query_arg( array('order_by' => 'From_Price') ) ?>">Highest</a>
</span>

<span class="clearfilter"><a href="<?php echo add_query_arg( array('order_by' => ' ') ) ?>">Clear Filter</a>
</span>

2011-10-31

Gabriel Reguly answers:

Hi wan,

Looks like Luis got the correct answer for the front end, but your code needs another fix, so instead of


register_custom_queryable_field("From_Price", array("dataType"=>"numeric","order"=>"DESC"));


please try

register_custom_queryable_field("From_Price", array("dataType"=>"numeric") );

Regards,
Gabriel