[Function] Get Count of Other CPT Items in the Current Item’s Taxonomies
Scenario:
- CPT:
attorney
- Associated taxonomy:
location
- Sample terms (hierarchical - checkboxes like categories): Tallahassee, Cape Coral, Orlando
- An attorney can have multiple locations.
- Sample terms (hierarchical - checkboxes like categories): Tallahassee, Cape Coral, Orlando
Requirement:
Create a condition that could be applied to any element on single attorney pages that will output it only if there is more than 1 attorney (other than the current one being viewed) in the locations that the attorney has been assigned to.
View: Single attorney pages
This members-only tutorial provides the PHP code for defining a custom function that
- finds all the location terms that the current attorney CPT has
- loops through the location terms and for each, get the attorney CPT items that have been assigned to that location and store this data in an array with the term ID being the key and post objects being the value
- flattens the array
- removes the duplicates from the array
- returns a count of the number of items in the array
This function can then be used with Oxygen's PHP Function Return value Dynamic Data to output a Section only if there is at least one other attorney in the location(s) that the current attorney has been assigned to.
Step 1
Add a new Code Snippet on the front end using a plugin like WPCodeBox.
Title: [Function] Get other attorneys count
Code:
This is a premium members-only content.
To view the rest of the content, please sign up for membership ($47/month or $599 one-time).
Already a member? Log in below or here.