Random Sort Settings

This topic contains 10 replies, has 3 voices, and was last updated by  Naveen Giri 4 years, 11 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: ,

  • Author
    Posts
  • #517016

    identity
    Lifetime Member
    Post count: 445

    Trying to understand random sorting.

    Read the snippet adjustment: https://wpgeodirectory.com/docs-v2/places/sorting/

    But still not sure or not seeing impact from any changes I’ve made in testing.

    Can you provide code or adjustment to that code needed to get:
    – randomization every 15 minutes
    – randomization every 30 minutes

    And a question, is there any negative impact to having this run at the default 24 hours vs every 30 minutes vs every 15 minutes?

    Cheers

    #517034

    Alex Rollin
    Moderator
    Post count: 27815
    #517043

    identity
    Lifetime Member
    Post count: 445

    Excellent, that helps!

    Cheers

    #517048

    identity
    Lifetime Member
    Post count: 445

    Unfortunately, not sure this is working.

    Trying both a ~3 minute limit as well as the “every refresh” version against the “tiered-featured” sort I was experimenting (from our other thread https://wpgeodirectory.com/support/topic/setting-up-two-levels-of-featured/#post-516997), but kept getting the same results.

    To validate that something else wasn’t influencing the results with that other test, I tried refreshing multiple times against a purely “random” sort with not sub-sorting or other treatment and I continue to get the results in the same order.

    Cheers

    #517068

    Alex Rollin
    Moderator
    Post count: 27815

    Please post the snippet you are using so we can take a look

    #517073

    identity
    Lifetime Member
    Post count: 445

    Using the recommended Snippets plugin, I tried individually, the following code set to run everywhere.

    First:

    add_filter('geodir_rand_seed','_my_geodir_rand_seed'); 
    function _my_geodir_rand_seed($rand_seed){
    $rand_seed = get_transient( 'geodir_rand_seed_custom' );
     
            // if we don't have a transient then set a new one
            if(!$rand_seed){
                $rand_seed = time();
                set_transient( 'geodir_rand_seed_custom', $rand_seed, .05 * HOUR_IN_SECONDS );
            }
     
            // validate
            $rand_seed = absint($rand_seed);
     
        return $rand_seed;
    }

    Then:

    add_filter('geodir_rand_seed','__return_zero'); // Upon refresh

    No sign of re-ordering even nearly 2 hours since I changed to the “Upon refresh” version.

    Thanks

    #517600

    Naveen Giri
    Moderator
    Post count: 1559

    Hi identity,

    where you checking it?

    I am considering you have enabled random sorting option.

    I have noticed, you have a . just before minute.
    please try it and check by sorting places in random sort.
    http://yoursite.com/places/?sort_by=random

    
    add_filter('geodir_rand_seed','_my_geodir_rand_seed');
    function _my_geodir_rand_seed($rand_seed) {
    	$rand_seed = get_transient( 'geodir_rand_seed_custom' );
    
    	// if we don't have a transient then set a new one
    	if ( ! $rand_seed ) {
    		$rand_seed = time();
    		set_transient( 'geodir_rand_seed_custom', $rand_seed,
    			5 * MINUTE_IN_SECONDS );
    	}
    
    	// validate
    	$rand_seed = absint( $rand_seed );
    
    	return $rand_seed;
    }
    

    Thanks

    #517680

    identity
    Lifetime Member
    Post count: 445

    Naveen,

    Thanks.
    I think the coding example here:
    https://wpgeodirectory.com/docs-v2/places/sorting/
    may have some errors in it.

    I noticed some differences in your code, beyond the MINUTE_IN_SECONDS vs the HOUR_IN_SECONDS that I was using from that page.

    My code, taken from https://wpgeodirectory.com/docs-v2/places/sorting/

    add_filter('geodir_rand_seed','_my_geodir_rand_seed'); 
    function _my_geodir_rand_seed($rand_seed){
    $rand_seed = get_transient( 'geodir_rand_seed_custom' );
     
            // if we don't have a transient then set a new one
            if(!$rand_seed){
                $rand_seed = time();
                set_transient( 'geodir_rand_seed_custom', $rand_seed, .05 * HOUR_IN_SECONDS );
            }
     
            // validate
            $rand_seed = absint($rand_seed);
     
        return $rand_seed;
    }

    VS your code:

    add_filter('geodir_rand_seed','_my_geodir_rand_seed');
    function _my_geodir_rand_seed($rand_seed) {
    	$rand_seed = get_transient( 'geodir_rand_seed_custom' );
    
    	// if we don't have a transient then set a new one
    	if ( ! $rand_seed ) {
    		$rand_seed = time();
    		set_transient( 'geodir_rand_seed_custom', $rand_seed,
    			5 * MINUTE_IN_SECONDS );
    	}
    
    	// validate
    	$rand_seed = absint( $rand_seed );
    
    	return $rand_seed;
    }

    Notice some differences between the two, especially in line 2 and line 6. Some of this may not matter, but I noticed that after adding a space in line two in my code between:
    ) {
    That my code appears to work now also.

    If that is the case, it would be helpful if someone could update the code example in the GD docs.

    I still haven’t been able to get the other “randomize on refresh” code to work:

    add_filter('geodir_rand_seed','__return_zero'); // Upon refresh

    Cheers

    #517685

    Naveen Giri
    Moderator
    Post count: 1559

    Hi,

    We will check and update the code, meanwhile instead of

    add_filter(‘geodir_rand_seed’,’__return_zero’); // Upon refresh

    please try

    add_filter(‘geodir_rand_seed’,’__return_false’); // Upon refresh

    Thanks

    #517696

    identity
    Lifetime Member
    Post count: 445

    No, that doesn’t seem to work either.

    That would be nice for testing, but at least this other code works now and the timing can be adjusted to a smaller delay for testing.

    Just to confirm and as I further edited the code for coding consistency and have tested and it appears to be working, this is the code that I am using now for randomization every 5 minutes.

    add_filter('geodir_rand_seed','_my_geodir_rand_seed'); 
    function _my_geodir_rand_seed($rand_seed) {
    	$rand_seed = get_transient('geodir_rand_seed_custom');
     
            // if we don't have a transient then set a new one
            if (!$rand_seed) {
                $rand_seed = time();
                set_transient('geodir_rand_seed_custom', $rand_seed, 5 * MINUTE_IN_SECONDS);
            }
     
            // validate
            $rand_seed = absint($rand_seed);
     
        return $rand_seed;
    }

    Which I have set to run in the Snippets plugin, set to Run Snippet Everywhere, though I imagine this could be set to just run on the Front-end.

    Cheers

    #517745

    Naveen Giri
    Moderator
    Post count: 1559

    thanks for confirming
    Yes, the code will run only for archive page queries.

Viewing 11 posts - 1 through 11 (of 11 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount