[venues] 'orderby' => 'rand'

This topic contains 18 replies, has 2 voices, and was last updated by  Stephen Harris 11 years, 3 months ago.

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #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
    #7631

    The function does support ‘rand’ its a wrapper for get_terms() (http://codex.wordpress.org/Function_Reference/get_terms) and only supports the orderby 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
    #7659

    So 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
    #7662

    You can, put you’d have to handle the actual pagination yourself (eo_get_venues() supports offset and number 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
    #7674

    So, if I understand correctly, I have to create these pages manually?

    Iurii Smuglov
    #7676

    No, 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 use add_rewrite_rule().

    Stephen Harris
    #7772

    Thanks 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
    #7782

    Currently 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
    #7889

    Ok. 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
    #7890

    That’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 by meta_value with meta_key order.

    Stephen Harris
    #7901

    No, thats not randomness. Thats desires of the customer %)

    Iurii Smuglov
    #7907

    Cant 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
    #7918

    So 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
    #7949
    if (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
    #7990

    Please 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
Viewing 15 posts - 1 through 15 (of 19 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.