Forums Forums Search & Filter Pro show featured post at top of search results

Viewing 10 posts - 1 through 10 (of 12 total)
  • Anonymous
    #76900

    I have a custom post type ‘therapists’.
    I have an ACF field ‘featured therapists’
    If this field is selected, I want this post to show up first in the search results.
    There will be multiple posts with the field ‘featured therapists’ selected. I need them to show in a random order BEFORE the other search results (which do not have this field selected).
    The other search results should then be displayed afterwards, also in a random order.
    How should I go about working this out?

    Trevor
    #76955

    Random as an order is not a good idea as such unless you are showing only a few, on one page only (no pagination). as soon as pagination kicks in, in WordPress you get a random selection per page, which could easily include a post from the previous page.

    As to sorting, you should be able to achieve this in the Posts tab. Sort first by that field and working out whether ascending or descending gives you what you want..

    Anonymous
    #77333

    The sorting under Posts tab does work for this, thanks.
    However, I was using a function to enable randomisation WITH PAGINATION on wordpress, and the sorting under the Posts tab only works if I comment out this function. How do I make them work together?
    This is my function:

    session_start();
    add_filter('posts_orderby', 'edit_posts_orderby');
    function edit_posts_orderby($orderby_statement) {
    if(!is_admin()) {
        $seed = $_SESSION['seed'];
        if (empty($seed)) {
          $seed = rand();
          $_SESSION['seed'] = $seed; }
       $orderby_statement = 'RAND('.$seed.')';}  
       return $orderby_statement;}
    Trevor
    #77352

    Ah. I do not know if this is possible. I will have to ask Ross, the developer.

    Ross Moderator
    #77833

    Hi Rena

    I can’t verify your code will necessarily work, but I can’t see why not.

    In order to use your randomisation function, I would recommend to disable all default sorting in the posts tab and then use your function.

    Thanks

    Anonymous
    #78673

    Hi,
    My code does work, but I need it to work showing a random list of the posts which have ACF field ‘featured therapists’ checked? Then after that all the other search results (also random).
    If I disable all the default sorting in the posts tab how can I use my function to do this?
    (I am also using Relevanssi)

    Ross Moderator
    #79323

    Hi Rena

    So you want, at the top to show featured therapists (randomised) and then the rest of the results (also randomised, but excluding the the featured therapists)?

    This is a little out of scope of the functionality of S&F so you will have to write some custom code but I will give a possible idea on how to implement.

    1) Create a custom WP_Query, and find the IDs of all your featured therapists in your site
    2) Use our filter sf_edit_query_args to edit the query of S&F… – more info

    You will find $query_args['post__in'] will be an array of post IDs… these are actually the IDs from all the results of your search..! so you can re-arrange the array $query_args['post__in']

    You would use the IDs from 1) to see if they are in the post__in array, and then move these values to the top of the array.

    Just make sure the IDs are in the order you want, and put back into $query_args['post__in'] as an array.

    Then in S&F as mentioned, choose default (no sorting) in the post tab.

    This is an idea of how it could work but would involve writing a lot of custom code depending on your exact requirements – also, it does not take into consideration how to handle the scenario when you are using a sort field in your form also.

    Hope that helps!

    Best

    Anonymous
    #79403

    Thanks so much for your help.
    What do you mean by a ‘sort field’? Do you mean in the Search Form UI?
    I’m using ‘post meta’ and ‘search’ fields. You can see the filter on http://gethelpisrael.mgtestsite.com/therapist/?_sfm_medical_professional_type=Therapist So would I be ok then?
    Also, how would I add the code for the randomisation with the pagination?
    Could I leave the filter: add_filter(‘posts_orderby’, ‘edit_posts_orderby’);
    and add add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 ); with my custom code and they would both work?
    Or do I need to combine them into one function?

    Anonymous
    #79470

    I’ve started to work on the code by beginning with showing only the featured therapists.
    I’ve got the code

    add_filter( 'sf_edit_query_args', 'filter_featured_first', 20, 2 );
    function filter_featured_first( $query_args, $sfid ) {
    if($sfid==491)
    	{ $query_args = array('meta_query' => array(
            'relation' => 'AND', array('key' => 'featured', 'value' => 1 )       
            ), 'posts_per_page'   => 4
            );}	return $query_args;}

    However, I can’t get the pagination to work. The same posts are being shown on every page.
    How do I fix the pagination? And how do I get it to work with the above function edit_posts_orderby()?

    Ross Moderator
    #80002

    Hi Rena

    As I mentioned above… this is a bit out of scope of support.

    You are applying some custom code that might work with a regular WP Query, but you want to apply it to S&F query.

    As I have not written that code, I don’t fully understand how it works, so I’m unable to advise on how to integrate the two any further.

    I did take a look but I can’t offer any further insight I’m afraid – I would it may not be possible to apply that code to S&F.

    I will consider adding support for proper maintained randomised results in a future release.

    Best

Viewing 10 posts - 1 through 10 (of 12 total)