osclass / Osclass

With Osclass, get your own classifieds site for free. Build your own Osclass installation and start advertising real estate, jobs or whatever you want- in minutes!
http://osclass.org/
649 stars 342 forks source link

Randomizing Latest listings on Osclass #2330

Closed Arref closed 2 years ago

Arref commented 2 years ago

I would like to randomize the landing page's Latest Listings. Rather than show the lates listings by date, I would like to randomize the listings. I am told I can do a subquery using rand() I know there is View::newInstance()->_exportVariableToView( 'listType' , 'latestItems' );

in osclass\oc-content\themes\sigma\functions.php and in osclass\oc-includes\osclass\gui\main.php

But I cannot figure out how to make a random subquery. Can someone help?

Arref commented 2 years ago

I am guessing it has something to with this section of code from osclass\oc-includes\osclass\helpers\hItems.php

at line 1067 `/**

dev-101 commented 2 years ago

This project is dead, some forks exist, but they aren't actively maintained afaik either. Forum is offline, it had a lot of resources.

Code you can try is this one, put it inside theme's functions.php and replace it in your theme's main view file (exclude cat ID is for your inspiration only):

// random latest items + with images only + exclude specific category ID
function cust_latest_items_random_with_images() {
    if (osc_is_home_page()) {
        $mSearch = Search::newInstance();
        $query_elements = (array) json_decode($mSearch->toJson());
//      $excluded_id = 9; // if no permalinks enabled => use category IDs

        $mSearch->addJoinTable(count($query_elements['tables_join']), DB_TABLE_PREFIX . 't_item_resource r', DB_TABLE_PREFIX . 't_item.pk_i_id = r.fk_i_item_id', 'LEFT');
        $mSearch->addConditions("r.s_content_type LIKE '%%image%%'");
//      $mSearch->addConditions(DB_TABLE_PREFIX."t_item.fk_i_category_id <> " . $excluded_id);
        $mSearch->order('RAND()', null);
    }
}
osc_add_hook('init', 'cust_latest_items_random_with_images');
Arref commented 2 years ago

This is awesome and perfect! Thank you so much! I'll give this a study to see if I can understand how it works.

Arref commented 2 years ago

I also modified the related_listings() function to randomize the Related listings section of the item view page. @dev-101 You are awesome!