Loading


Advertisement:

Author Topic: Sort ads by custom field  (Read 1085 times)

VWS SA

  • Newbie
  • *
  • Posts: 1
Sort ads by custom field
« on: December 08, 2016, 08:44:31 pm »
Good day

Part of my website will have events listed on it. So users will be able to list their event flyer, information etc. I have added a custom field for the user to ad the event date and would like to have the ads show according to that date on the category pages and not sort the ads according to when it was published. If the system uses the published date its all mixed up.

Would greatly appreciate some help with setting the website up to sort the ads from the date custom field and not published date if possible :-)

If at all possible can i have the custom date field show-up on the ad preview when users are on the category pages?


teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #1 on: December 08, 2016, 09:42:14 pm »
Hi,

1.- To sort by date on custom field:

Add this at the very bottom of your theme functions.php:
Notes:
1.- Take care not to leave blank lines after this.
2.- If your theme functions.php doesn't end with ?> skip first line of my code.

Code: [Select]
<?php
function cust_orderListings_by_meta_date() {

    
$cf_identifier 'test-date'// Change custom field slug here
    
        
$mSearch =  Search::newInstance();
        
$query_elements = (array) json_decode($mSearch->toJson());
        
$joins count($query_elements['tables_join']);

        
$mSearch->addJoinTable($joinsDB_TABLE_PREFIX.'t_item_meta c'DB_TABLE_PREFIX.'t_item.pk_i_id = c.fk_i_item_id''LEFT');
        
$mSearch->addJoinTable($joins+1DB_TABLE_PREFIX.'t_meta_fields mf''mf.pk_i_id = c.fk_i_field_id''INNER');
        
$mSearch->addConditions(sprintf("mf.s_slug = '%s'"$cf_identifier));
        
$mSearch->order("c.s_value DESC, dt_pub_date""DESC");  
}

osc_add_hook ('search_conditions','cust_orderListings_by_meta_date');  
?>


Just replace 'test-date' with the real identifier of your Event custom field.

2.- To be able to show the value of a given custom field on your Search list see here:

http://forums.osclass.org/themes/problem-on-the-call-two-time-meta-field/msg106769/#msg106769

Regards

fog

  • Hero Member
  • *****
  • Posts: 889
Re: Sort ads by custom field
« Reply #2 on: December 09, 2016, 06:27:23 pm »
Hi teseo :) is not my intention change the context of topic, I think is related partially.

I not have custom field, I'm using a field to insert date with name and column: 's_startdate' on table t_item_events (is not a plugin is sql imported on DB).

Is possible some solution with a function to filter by minimum date?
I mean, user will using main query field to searching by minimum date.

Per example, somebody start search with a min date of: 2016-12-13, and the search results begin with that minimum date.

Some function you can share as solution, or it can be more complicated without other parameters? Some suggestion?

Thanks in advance
Regards

teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #3 on: December 09, 2016, 07:43:28 pm »
Hi fog,

Try this: ???

(Assuming the received parameter with the minimum date is called "startdate", change that).

Code: [Select]
<?php
function cust_events_search_conditions() {
    
$start_date Params::getParam('startdate');
    if (
$start_date) {
        
$mSearch Search::newInstance();
        
$query_elements = (array)json_decode($mSearch->toJson());
        
$joins count($query_elements['tables_join']);

        
$mSearch->addJoinTable($joinsDB_TABLE_PREFIX 't_item_events e'DB_TABLE_PREFIX 't_item.pk_i_id = e.fk_i_item_id''LEFT');
        
$mSearch->addConditions(sprintf("e.s_startdate >= '%s'"$start_date));
    }
}

osc_add_hook ('search_conditions','cust_events_search_conditions');

Regards
?>


fog

  • Hero Member
  • *****
  • Posts: 889
Re: Sort ads by custom field
« Reply #4 on: December 09, 2016, 08:26:35 pm »
Hi teseo,

I changed to 's_startdate':
Code: [Select]
$start_date = Params::getParam('s_startdate');
The link working well directly: index.php?page=search&s_startdate=

I assume is needed a occult field with that link as value? Something like:
Code: [Select]
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" onsubmit="return doSearch()" class="nocsrf">
<input type="hidden" name="page" value="search&s_startdate=" />
<fieldset>

value="search&s_startdate="

I'm sorry, I'm sick and I can hardly even think straight.

Thank you so much for your function

teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #5 on: December 09, 2016, 08:40:58 pm »
Why a hidden field? It's not supossed the user to input a date? ???

Anyway it should be:

Quote
<input type="hidden" name="s_startdate" value="[Some date]" />

Regards

fog

  • Hero Member
  • *****
  • Posts: 889
[solved] Sort ads by other specific value field
« Reply #6 on: December 09, 2016, 08:51:28 pm »
Sorry... my mistake, I meant by name="s_startdate". Yes, no need hidden field, other my mistake, it will working with calendar and the field cannot be edited manually, the date to select will appears bigger than current time(), so is the essencial.

Thanks again. I think is solved. I will do my changes later, when I feel better.

Have a good weekend!  :)
« Last Edit: December 09, 2016, 08:53:20 pm by fog »

teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #7 on: December 09, 2016, 09:06:05 pm »
You're welcome, get well soon. :)

fog

  • Hero Member
  • *****
  • Posts: 889
Re: Sort ads by custom field
« Reply #8 on: December 10, 2016, 03:34:44 am »
Hi teseo thanks for your kind words.  :)

I just saw now, the listings on search results the minimum date start in bottom of listings.  ???

I really needed search by order of recent date of "s_startdate". Example:
2016-12-11
2016-12-13
2016-12-24

Would be nice if you can reverse the order.

Thank you in advance!
Regards

teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #9 on: December 10, 2016, 01:06:45 pm »
Just add this line:

Code: [Select]
$mSearch->order("e.s_startdate", "ASC");
Regards

fog

  • Hero Member
  • *****
  • Posts: 889
Re: Sort ads by custom field
« Reply #10 on: December 10, 2016, 06:14:41 pm »
Ah ... I was very close to that ... I forgot to 'e.' on column.

Thank you again man  :D
Regards

fog

  • Hero Member
  • *****
  • Posts: 889
Re: Sort ads by custom field
« Reply #11 on: December 12, 2016, 09:45:34 pm »
Hi teseo, sort by tradicional methods not working anymore if user use sort by date, unless user remove filter by date (start new search without date filter) but it can confuse the user.

Function have priority on code, and not avoid it when exists other type by order.

Example: search/s_startdate,2016-12-12/sOrder,i_price/iOrderType,asc

Can it be avoided? Unless in alternative clicking by tradicional method can avoid this line on function:
Code: [Select]
$mSearch->order("e.s_startdate", "ASC");
something like: if (! order by price on search link, or sort by newly){ show the line to order by s_startdate "ASC"}

What is better: some conditional to avoid the line code, or is possible join all together? Not sure if is possible join all (can be much complex).

If you know something related, let me know.

Thank you again for your time.
Regards

teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #12 on: December 13, 2016, 02:26:31 pm »
if (! order by price on search link, or sort by newly){ show the line to order by s_startdate "ASC"}

The problem is that "sort by newly" is added by default on any search made from the sidebar (unless "by price" has been selected earlier), so no way to know if the user has expressly chosen it. I guess you could add a new hidden input as a flag of sorts that would be filled when the user expressly chooses an Order method, but that you'd need to program yourself and add it to the theme...

If you are capable to do that, I might adapt this function.

Regards

fog

  • Hero Member
  • *****
  • Posts: 889
Re: Sort ads by custom field
« Reply #13 on: December 13, 2016, 10:17:36 pm »
Hi teseo, sorry just now, complicated day to me.  :o

You try tell me it will working if exists a value in a hidden field selected? And that value can be controled by name or id. That selection can be dynamical by conditional (just thinking).

Let's suppose I have already that occult field pre selected value="1", with name and id named 'ascStartdate'.

Search was submited, '1' as value now, and search results.
On menu (sort by), user clicked on lower price first and occult field have '0' as value now (using javascript).

Unless values are volatile after refresh page by link, I never tried do it before. Is needed keep values in a session?

Well, if is needed, I think function will check to compare the value of occult field (keep him in session), and will run without the last code line to s_startdate 'asc'.  Revert again, using a direct link on menu, and add again the value="1" and keep the value again on same session.

Don't know how far it can be possible from my side.

Maybe I not understand yet your strategy, and can avoid my doubts.
Let me know when you can
Thanks for all your effort on help to solve this point.
Regards

teseo

  • Hero Member
  • *****
  • Posts: 5641
Re: Sort ads by custom field
« Reply #14 on: December 20, 2016, 02:48:40 pm »
Hi fog,

Sorry for the delay, very busy these days.

I'm assuming this you want it working at the theme level, so I think the best solution would be this one:

Once the user enters a date for the event, use jQuery to alter hidden inputs sOrder and iOrderType on the form element in the left sidebar, then:

Code: [Select]
<?php
function cust_events_search_conditions() {
    if (
Params::getParam('sOrder') == 'ascStartdate') {

Didn't tested myself, but I think this way, when search page is refreshed those hidden inputs would keep the order until the user expressly select a different kind of order... ???

Regards