Skip Navigation

[Resolved] Displaying posts from the parent category of a page

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a View that displays CPTs from a different post type that have the same lowest-level taxonomy term as the current CPT or term archive page. It should be dynamic so it can be included in a widget on many different CPTs and archives.

Solution: Add the following custom shortcode, which will return the lowest-level taxonomy term for a specific CPT or Archive:

add_shortcode( 'ts_deepest_location', 'ts_deepest_location_func');
function ts_deepest_location_func($atts)
{
  $id = $atts['id'];
  $terms = wp_get_post_terms( $id, 'locations', array( 'orderby' => 'id', 'order' => 'DESC' ) );
  $deepestTerm = false;
  $maxDepth = -1;
  foreach ($terms as $term) {
      $ancestors = get_ancestors( $term->term_id, 'locations' );
      $termDepth = count($ancestors);
      if ($termDepth > $maxDepth) {
          $deepestTerm = $term;
          $maxDepth = $termDepth;
      }
  }
  return $deepestTerm->slug;
}

To display the View on a CPT, use this shortcode in a shortcode argument for a View filtered by taxonomy:

[wpv-view name="featured-breaks-for-markets-view" wpvlocations="[ts_deepest_location id="[wpv-post-id id='$current_page']"]"]

To display the View on a taxonomy Archive, use a wpv-taxonomy-archive shortcode in a shortcode argument for a View filtered by taxonomy:

[wpv-view name="featured-markets-for-locations-view" wpvlocations="[wpv-taxonomy-archive info='slug']"]

Be sure to register the ts_deepest_location function in Toolset > Settings > Front end Content > 3rd Party Shortcode arguments.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

This support ticket is created 7 years, 5 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 22 replies, has 3 voices.

Last updated by davidR-12 7 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#541616

I see, that's because this part won't work in an archive:

[ts_deepest_location id="[wpv-post-id id='$current_page']"]

I assume this is a location term archive page, correct? Then basically you want the slug of the current archive page instead of the slugs associated with the current post. In order for this to work, you must replace the inner shortcode to use the [wpv-taxonomy-archive] shortcode here:

[wpv-view name="featured-markets-for-locations-view" wpvlocations="[wpv-taxonomy-archive info='slug']"]

[wpv-taxonomy-archive info='slug'] should output 'uk' on this page, since it's the term slug for this archive. This string can be consumed by the wpvlocations parameter of your View.

This should work at multiple levels of hierarchy. Please let me know the results - I would implement it myself but at first glance I can't tell where the shortcode should be placed to show up here.

#544554

Hi Christian,

Everything looks great and is working. I'm waiting to hear back form the client though so will keep this thread open until I have heard back form them.

Regards,

David

#544802

Shane
Supporter

Languages: English (English )

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

Hello,

Christian is currently unavailable due today but will be back on Wednesday to check in on this ticket.

Thanks,
Shane

#545124

Hi Christian,

I have migrated the toolset views to the live site but am having an issue with the views widgets that reference the ts_deepest_location function. You can see the issue here:

hidden link

The 2 views 'featured breaks in this location' and 'breaks in this location' are displaying only the final ' "] ' part of the shortcode. I have copied them exactly form the staging site.

Can you help me with this.

Regards,

David

#545364

When you migrated the code and widgets, did you also migrate the 3rd-party shortcode argument registration in Toolset > Settings > Front-end Content > Third-party shortcode arguments? This would be my first suggestion to check. If the ts_deepest_location shortcode is not included, add it. If it's already there , then we need to understand which part of the solution is breaking down. I would try adding each piece of the shortcode independently to see what works and what breaks:

<div style="display:none;">[ts_deepest_location id="1234"]</div>

Modify 1234 to manually match the ID of this market as a test. See if the deepest term is displayed inside the hidden div.

Next include the View shortcode without the ts_deepest location shortcode:

<div style="display:none;">[wpv-view name="featured-breaks-for-markets-view" wpvlocations="manchester"]</div>

Look for the output of this View and see if any results are shown.

Let's see if either of those work as expected and go from there.

#545598

That was it! I hadnt registered the shortcode. Thanks Christian. Just waiting to hear back form the client but that should be it now.

David

#545639

Great, I'll mark this ticket as pending an update from you and we can keep working on it as needed.

#549820

Hi Christian,

Thank you for all your help. All singed off with the client now.

Regards,

David