• Greetings. I was trying to add the option to randomize the listing through the shortcode (using a duplicate of it and setting on functions.php) I was trying to add the parameter like this but i have no results.
    [On the original file is around the line 120 of wpadverts/includes/shortcodes.php]

    if( isset( $sarr[1] ) && isset( $sort_options[$sarr[0]]["items"][$adverts_sort] ) ) {
    
            $sort_key = $sarr[0];
            $sort_dir = $sarr[1];
    
            if($sort_dir == "asc") {
                $sort_dir = "ASC";
            } elseif ($sort_dir == "rand") {
                $sort_dir = "RAND";
            }else {
                $sort_dir = "DESC";
            }
    
            if($sort_key == "title") {
                $orderby["title"] = $sort_dir;
            } elseif($sort_key == "date") {
                $orderby["date"] = $sort_dir;
            } elseif($sort_key == "rand") {
                $orderby["rand"] = $sort_dir;
            }elseif($sort_key == "price") {
                $orderby["adverts_price__orderby"] = $sort_dir;
                $meta["adverts_price__orderby"] = array(
                    'key' => 'adverts_price',
                    'type' => 'NUMERIC',
                    'compare' => 'NUMERIC',
                );
            } else {
                // apply sorting using adverts_list_query filter.
            }
            //if($sort_key == "random") {echo "random";}
    
            $sort_current_text = $sort_options[$sort_key]["label"] ;
            $s_descr = $sort_options[$sort_key]["items"][$adverts_sort];
            $sort_current_title = sprintf( __( "Sort By: %s - %s", "wpadverts"), $sort_current_text, $s_descr );
        } else {
            $adverts_sort = $order_by;
            $orderby["date"] = "desc"; 
        }

    And i see that later i can switch the ordering to random directly but the idea is having the option instead of switching on the functions.
    [On the original file is around the line 155 of wpadverts/includes/shortcodes.php]

    $args = apply_filters( "adverts_list_query", array( 
            'author' => $author,
            'post_type' => 'advert', 
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page, 
            'paged' => $paged,
            's' => $query,
            'meta_query' => $meta,
            'tax_query' => $taxonomy,
            'orderby' => $orderby
        ), $params);

    Where 'orderby' => $orderby i can shange it to 'orderby' => 'rand' but the idea is setting the random parameter on the first chunk of the code.

    I have the impression that i missed somethnig but I’m lost.

Viewing 1 replies (of 1 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    you can enable sorting by random ad by adding the code below in your theme functions.php file

    
    add_filter( "adverts_list_query", function( $args, $params ) {
        if( isset( $params["order_by"] ) && $params["order_by"] == "rand" ) {
            $args["orderby"] = array( "rand" => "DESC" );
        }
        return $args;
    }, 10, 2 );
    

    Now if you use the [adverts_add] like this

    
    [adverts_add order_by="rand"]
    

    If you would like to have an option for users to select sorting by “random” then i am afraid that right now this is not really possible (at least not without customizing original source code).

Viewing 1 replies (of 1 total)

The topic ‘Add Random Sorting to Adverts Lists’ is closed to new replies.