Skip Navigation

[Closed] Shortcode to sort posts

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 3 voices.

Last updated by Christian Cox 4 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1561813

Ok Christian, i'll try like this thank.

In the while, because you helped me in the past with the code can i take this opportunity to ask you about a toolset code integration ?

I would help to integrate a shortcode to order posts based on a custom field. I Would create a view in a location cpt to sort nearest location(s) to that.

#1562701

I'm not sure I understand, can you provide some more details about how this would work?

#1562725

I have a "location" post type
I would create a view inside the location post template

This views shows the nearest location to the actual ordered by distance.
I can't see any views to choose a custom field as distance from this, so i would create an extension to this

#1562993
Screen Shot 2020-03-25 at 1.55.59 PM.png

You can use a distance filter set by a shortcode attribute to pass in the current post's address field:

[wpv_view name="Your View Name" mapcenter="[types field='your-address-field-slug' output='raw'][/types]"]
#1563129
Schermata 2020-03-25 alle 20.55.22.png

I need to set the ordering but it shows me just 3 option (fixed, visitor location, url parameter)

For the filtering it is ok

#1563157

I need to create an ordered list by distance from a custom field

#1564373

I understand better now, thank you. You would like to filter by distance and order the results by distance as well. It requires a bit of custom code, and I helped another client with a similar setup. Please review this forum ticket and let me know if you have questions about implementing this solution: https://toolset.com/forums/topic/sorting-views-by-distance-to-current-post-geolocation/

I'll be glad to give you more feedback if you have concerns.

#1569191

Hi Christian,
i tryed the code but is still doesn't show the distance from the location but by my locatiom

#1570011

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alessandro,

Christian is currently unavailable today but he will be back tomorrow to continue assisting with this.

Thanks,
Shane

#1571241

Can you share all the code you used so I can see the exact changes you made?
1. The wpv-view shortcode used to display the map
2. The toolset-maps-distance-view shortcode used to display the distance
3. The full PHP code snippet to filter wpv_view_settings

I will review the changes and give you some feedback.

#1571277
Schermata 2020-04-01 alle 23.07.46.png

Plugin:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_view_settings', 'ts_sort_map_by_distance_from_shortcode', 99, 2 );
function ts_sort_map_by_distance_from_shortcode( $view_settings, $view_id ) {
  global $WP_Views;
  $views = array( 362 );
  if ( in_array( $view_id, $views) ) {
    $shortcode_atts = isset( $WP_Views->view_shortcode_attributes[0] ) ? $WP_Views->view_shortcode_attributes[0] : null;
    if ( isset($shortcode_atts['from_center']) ) {
      $query_center = $shortcode_atts['from_center'];
      $view_settings['distance_order'] = array(
        "source" => "fixed",
        "center" => $query_center
      );
    }
  }
  return $view_settings;
}

View:

Distanza da [wpv-post-title]: [toolset-maps-distance-value location='[wpv-attribute name="from_center"]' postmeta='wpcf-indirizzo-location']
<div class="card">
  <a href="[wpv-post-url]"><img class="card-img-top" src="<em><u>hidden link</u></em>" alt="[wpv-post-title]"></a>
    
    <div class="card-body">
      <h5 class="card-title"><a href="[wpv-post-url]">[wpv-post-title]</a></h5>
      <p class="card-text">[types field='indirizzo-location'][/types]</p>
      <p class="card-text"><small class="text-muted">[toolset-maps-distance-value origin_source='visitor_location' postmeta='wpcf-indirizzo-location'] Km</small></p>
    </div>
  </div>

Shortcode:

[wpv-view name='locali-intorno-a-questo' from_center='[wpv-post-field name=wpcf-indirizzo-location'] ']

#1572365

Hi, the locali-intorno-a-questo View shortcode seems to be formatted incorrectly, which will cause a problem with the distance calculation. Please try this change:

[wpv-view name='locali-intorno-a-questo' from_center='[wpv-post-field name="wpcf-indirizzo-location"] ']

If this does not resolve the problem, I'll be glad to take a closer look in wp-admin. Please provide login credentials here and let me know where I can see this content on the front-end of the site.

The topic ‘[Closed] Shortcode to sort posts’ is closed to new replies.