“total_pods”, but with a relationship field filter
-
Hi!
I use a counter, based on total_pods, but I need to build in a filter for a relationship field.
There are two pods, SONGS and CONCERTS. The SONG pod includes data like song title, lyrics etc., the CONCERT pod includes data like date of the event, venue and city, and what song (from the SONG pod) will be performed.
On the SONG page/template, the total number of concerts that include that very song, shall be displayed. What I have already, is this counter using total_pods:
<?php function total_pods($atts , $content = null ) { extract(shortcode_atts( array ( 'my_pod' => '' ), $atts)); $my_pods = pods( $my_pod , array( 'limit' => -1 ) ); return $my_pods->total_found(); } add_shortcode( 'count_pods', 'total_pods' ); ?> <p>Number of Concerts: [count_pods my_pod ='concerts']
It succeeds to display the number of concerts, but the number represents the total number of concerts from the CONCERT pod – whatever songs were performed.
How could I restrict this number to only count those concerts, which feature that song on whose page/template the number is displayed?
The Pods Documentation features this article, Get Values from a Custom Relationship Field. Maybe this $related = $pod->field( ‘relationship_field’ ); bit could be the solution for me? But I would not know how to do that.
<?php //get Pods object for current post $pod = pods( 'pod_name', get_the_id() ); //get the value for the relationship field $related = $pod->field( 'relationship_field' ); //loop through related field, creating links to their own pages //only if there is anything to loop through if ( ! empty( $related ) ) { foreach ( $related as $rel ) { //get id for related post and put in ID //for advanced content types use $id = $rel[ 'id' ]; $id = $rel[ 'ID' ]; //show the related post name as link echo '<a href="'.esc_url( get_permalink( $id ) ).'">'.get_the_title( $id ).'</a>'; //get the value for some_field in related post and echo it $someField = get_post_meta( $id, 'some_field', true ); echo $someField; } //end of foreach } //endif ! empty ( $related ) ?>
Thank you for your help. (As you can see, I am not really experienced with coding.)
Best,
Stefan
- The topic ‘“total_pods”, but with a relationship field filter’ is closed to new replies.