orderby gives a comma separated list
-
Hi,
I am trying to make a list of regions containing organizations that are sometimes related to the same region.
For the regions I created a taxonomy »regions“. With oderby=”region” I get an output like: region01, region02, region03, and region04 followed by the organization.I want to have each region listed individually with its connected organization even if the organizations repeat.
Can I do this with a different »orderby«?
Thank you for the great plugin!
The page I need help with: [log in to see the link]
-
Using “Connections” tab to connect a Region taxonomy with Organization post type; outputting organizations by ordered regions with shortcode:
<?php /** * Plugin Name: Shortcode - Orgs by Region * Description: Get all Organizations by taxonoomy Region with shortcode <code>[orgs_by_region]</code>. */ add_shortcode( 'orgs_by_region', function(){ ob_start(); // @see https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ $regions = get_terms([ 'taxonomy' => 'region', 'orderby' => 'name', 'hide_empty' => 'false', ]); echo '<ul>'; foreach( $regions as $region ) { // @see https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters $orgs_in_this_region_arr = get_posts( [ 'post_type' => 'organization', 'posts_per_page' => -1, 'tax_query' => [ [ 'taxonomy' => 'region', 'field' => 'term_id', 'terms' => $region->term_id, 'include_children' => false, ] ] ]); if ( ! empty( $orgs_in_this_region_arr ) ) { $orgs_in_this_region = '<br/>'; foreach( $orgs_in_this_region_arr as $org ) { $orgs_in_this_region .= sprintf( '<a href="%s">%s</a> ', esc_url( get_permalink( $org->ID ) ), get_the_title( $org->ID ) ); } }else { $orgs_in_this_region = ''; } printf( '<li>%s%s</li>', $region->name, $orgs_in_this_region ); } echo '</ul>'; return ob_get_clean(); } );Thank you for your response. The website is set up in german and I used »landkreis« in the taxonomy for the »region« and »aidshilfe« for the »organizations«.
Since I am not familiar with php … do I replace all »region« by »landkreis« and post type »organization« by »aidshilfe«?
I guess there is no simpler way? 😉
@blindtexth Ja, richtig.
Dies ist ein einfacher weg. 😉<?php /** * Plugin Name: Shortcode - Orgs by Region * Description: Get all Aidshilfe by taxonomy Landkreis with shortcode <code>[orgs_by_region]</code>. */ add_shortcode( 'orgs_by_region', function(){ ob_start(); // @see https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ $regions = get_terms([ 'taxonomy' => 'landkreis', 'orderby' => 'name', 'hide_empty' => 'false', ]); echo '<ul>'; foreach( $regions as $region ) { // @see https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters $orgs_in_this_region_arr = get_posts( [ 'post_type' => 'aidshilfe', 'posts_per_page' => -1, 'tax_query' => [ [ 'taxonomy' => 'landkreis', 'field' => 'term_id', 'terms' => $region->term_id, 'include_children' => false, ] ] ]); if ( ! empty( $orgs_in_this_region_arr ) ) { $orgs_in_this_region = '<br/>'; foreach( $orgs_in_this_region_arr as $org ) { $orgs_in_this_region .= sprintf( '<a href="%s">%s</a> ', esc_url( get_permalink( $org->ID ) ), get_the_title( $org->ID ) ); } }else { $orgs_in_this_region = ''; } printf( '<li>%s%s</li>', $region->name, $orgs_in_this_region ); } echo '</ul>'; return ob_get_clean(); } );Danke für den »einfachen« Weg.
One more stupid question: How do I implement the shortcode [orgs_by_region] into the page?
So far I used:
[pods name=”aidshilfe” orderby=”landkreis” limit=”-1″ template=”Aidshilfen Liste”]Hi @blindtexth
A shortcode can be pasted in any content field that parses shortcodes.
This depends a bit on your theme however.See also: https://developer.wordpress.org/reference/functions/apply_shortcodes/
Cheers, Jory
Hi Jory,
Yes, I know where to put the shortcode … I was not sure, which one to use.
So far I used:
[pods name=”aidshilfe” orderby=”landkreis” limit=”-1″ template=”Aidshilfen Liste”]
What do I do with the [orgs_by_region] ?
Cheers, Carsten
It would replace your pods shortcode.
What you are trying to do (grouping by taxonomy) simply cannot be done with the Pods shortcode.Cheers, JOry
Hi Jory,
I managed to place the php and have a list. Unfortunately it looks completely different.
On this test page you can see the list first and below the horizontal line the original list and layout.https://niedersachsen-aidshilfe-migration.de/test/
Just wondering, if it is at all possible to have the second listing in it’s layout with the Organizations sorted by region or to have the first example look like the second example.
Cheers
CarstenHi @blindtexth
The code is a starting point for you to continue your work on. Currently it will output a list of items, see the last part in the script:
printf().Using a similar script you could also fetch data based on other fields and taxonomies.
However, custom development is not in the scope for Pods support.
This isn’t as much as a Pods core feature as more a general PHP/WordPress core development functionality.
More documentation:
https://developer.wordpress.org/reference/functions/get_posts/
https://developer.wordpress.org/reference/classes/wp_query/With PHP everything is possible 😉
Cheers, Jory
Hi Jory,
thank you for your time … I did not expect, listing pods content would be a custom development. I appreciate your effort and explanations. Thank you.
Cheers, Carsten
Hi @blindtexth
Listing content (custom post types etc.) of course wouldn’t necessary be custom development.
However, the way you want to query this content is and output/format the content is.There are many many ways to output content, using Pods or even WordPress core functionality.
You can even run the query results through a Pod template again so you can format all your posts within Pods.
Example:$posts = get_posts('YOUR CUSTOM QUERY HERE, SEE EXAMPLE IN PREVIOUS COMMENTS'); foreach ( $posts as $post ) { $pod = pods( $post->post_type, $post ); echo $pod->template( 'YOUR PODS TEMPLATE NAME' ); }Within these templates you can use our magic tags:
https://docs.pods.io/displaying-pods/magic-tags/using-magic-tags/What I’m trying to get to is that there are many ways to display and/or format your output. You’ll have to find the way that suits your needs best.
As for the design/UI of the output, you could try to replicate the original output with these results or you can create your own CSS classes to use and create your own layout.Cheers, Jory
-
This reply was modified 4 years, 1 month ago by
Jory Hogeveen.
-
This reply was modified 4 years, 1 month ago by
Jory Hogeveen.
-
This reply was modified 4 years, 1 month ago by
The topic ‘orderby gives a comma separated list’ is closed to new replies.