[venues] 'orderby' => 'rand'
WordPress Event Management, Calendars & Registration › Forums › General Question › [venues] 'orderby' => 'rand'
This topic contains 18 replies, has 2 voices, and was last updated by Stephen Harris 11 years, 3 months ago.
-
AuthorPosts
-
October 8, 2013 at 12:03 pm #7624
Hi, Stephen.
I tried to sort the places randomly, but no effect$venues = eo_get_venues( array( "orderby" => "rand", "number" => 9, "meta_query" => $venue_query) );
Number is working fine, but not rand
Iurii Smuglov
October 8, 2013 at 2:18 pm #7631The function does support ‘rand’ its a wrapper for
get_terms()
(http://codex.wordpress.org/Function_Reference/get_terms) and only supports theorderby
arguments there.Depending on the number of venues you could randomly shuffle the returned venues:
$venues = eo_get_venues( array( "orderby" => "rand", "number" => 9, "meta_query" => $venue_query )); shuffle( $venues ); //$venues is now sorted randomly.
Stephen Harris
October 9, 2013 at 8:39 am #7659So is there a way to get some kind of pagination for Venues? Cuz i have a lot of them, and its not really nice to show more than 100 venues on 1 page 🙂
Iurii Smuglov
October 9, 2013 at 2:41 pm #7662You can, put you’d have to handle the actual pagination yourself (
eo_get_venues()
supportsoffset
andnumber
arguments). There is no list of venues by default.$page = 7; //page you want $number = 10;//number of venues to display $venues = eo_get_venues( array( 'number' => $number, 'offset' => ($page-1) * $number, ));
Stephen Harris
October 9, 2013 at 3:38 pm #7674So, if I understand correctly, I have to create these pages manually?
Iurii Smuglov
October 9, 2013 at 3:49 pm #7676No, but you would need to create the code so the pages are generated dynamically.
So for instance, this gist gets you part way there by listing all venues one a page: https://gist.github.com/stephenharris/4988324 (create a page using that as the tempate)
You can adapt that to include
$page = ( isset( $_GET['v-page'] ) ? intval( $_GET['v-page'] ) : 1 ); $page = max( $page, 1 ); $number = 10;//number of venues to display $venues = eo_get_venues( array( 'number' => $number, 'offset' => ($page-1) * $number, ));
rather than just
$venues = eo_get_venues();
Then
www.yoursite.com/yourvenuepage/?v-page=2
would give you page 2 of your venues. If you want ‘pretty permalinks’ then you’ll need useadd_rewrite_rule()
.Stephen Harris
October 16, 2013 at 10:49 am #7772Thanks Stephen.
Could you give me an advise? Can i change order of venues manually? Maybe with some metafields (rating system, order number or something else). I am out of ideas how to deal with that list of Venues. 🙁In other words:
I have page with more than 100 venues. And i need to display only couple of them in random order, or set order manually.P.S. getting a few venues and them shufling them isnt a great idea. Those venues anyway are first (or last) in alphabet
Iurii Smuglov
October 16, 2013 at 11:22 pm #7782Currently you can’t sort venues by meta value (something for 1.5 I think), but the following code adds that:
function my_support_metavalue_order( $pieces, $taxonomies, $args ) { global $wpdb; if ( !in_array( 'event-venue', $taxonomies ) || empty( $args['meta_query'] ) ) return $pieces; if( 'meta_value' == $args['orderby'] ){ $pieces['orderby'] = "ORDER BY $wpdb->eo_venuemeta.meta_value"; }elseif( 'meta_value_num' == $args['orderby'] ){ $pieces['orderby'] = "ORDER BY $wpdb->eo_venuemeta.meta_value.meta_value+0"; } return $pieces; } add_filter( 'terms_clauses', 'my_support_metavalue_order', 15, 3 );
Then to sort by meta key ‘_city’ (which is the city field of the venue):
$venue_query = array( array( 'key' => '_city', 'compare' => 'EXISTS' ) ); $venues = eo_get_venues( array( 'orderby' => 'meta_value', 'meta_query' => $venue_query ) );
In a similar way you could sort the venues randomly with
ORDER BY RAND()
. But random sorting cannot be done in conjunction with pagination for obvious reasons.Stephen Harris
October 24, 2013 at 1:31 pm #7889Ok. Now order has changed, but i`m unable to control it. Can i just manually set number of page where venue should display for each venue?
Iurii Smuglov
October 24, 2013 at 1:44 pm #7890That’s randomness for you 🙂
Just give each venue a meta value
order
, with an integer setting the order in which they should appear (1 – first, 2 – second, 3- third and so on.). Then order bymeta_value
withmeta_key
order
.Stephen Harris
October 24, 2013 at 7:39 pm #7901No, that
s not randomness. That
s desires of the customer %)Iurii Smuglov
October 25, 2013 at 11:09 am #7907Cant figure out what exactly i should write 🙁 Looks like I`m noob 🙁
I need pagination and metafield (already have created simple text metafield _order) for venue with priority number
-
This reply was modified 11 years, 4 months ago by
Iurii Smuglov.
Iurii Smuglov
October 26, 2013 at 2:43 pm #7918So assuming if you’ve included the first snippet of 7782 in your
functions.php
you can then query venues by order:$venue_meta_query = array( array( 'key' => ' _order', 'compare' => 'EXISTS' ) ); $venues = eo_get_venues( array( 'orderby' => 'meta_value', 'meta_query' => $venue_meta_query ) );
If you’ve got additional venue-meta queries (you can have more than one), the
'orderby'=>'meta_value'
refers to the meta query that appears first in the$venue_meta_query
array.Stephen Harris
October 28, 2013 at 10:27 am #7949if (is_category(6)): $venue_query = array( array('key' => '_cat', 'value' => 'BARS / PUBS',), array ('key' => '_big', 'value' => 'no'), array( 'key' => '_order', 'compare' => 'EXISTS' ) ); if (is_category(6)): $page = ( isset( $_GET['v-page'] ) ? intval( $_GET['v-page'] ) : 1 ); $page = max( $page, 1 ); $number = 12;//number of venues to display $venues = eo_get_venues( array( 'number' => $number, 'offset' => ($page-1) * $number, 'orderby' => 'meta_value', 'meta_query' => $venue_query) );
Now I see all the Venues that have a number,
but they are displayed in a strange order,
and number change does not affect the output.And one more question. Can i set default order number (1000 for example), so new venues will create at the end of list if i forgot to set my own number?
Iurii Smuglov
October 30, 2013 at 5:07 pm #7990Please see my comment in the above thread 🙂
If you’ve got additional venue-meta queries (you can have more than one), the ‘orderby’=>’meta_value’ refers to the meta query that appears first in the $venue_meta_query array.
Try re-ordering where
_order
and_cat
appear…Stephen Harris
-
This reply was modified 11 years, 4 months ago by
-
AuthorPosts