• Resolved Stefancom

    (@stefancom)


    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hello @stefancom

    You already answered your own question in this case ??
    The field() method is indeed the best way to do this.
    Just take your example and instead of the foreach loop you can use count() (https://www.php.net/manual/en/function.count.php) to get the total related items.

    Cheers, Jory

    Thread Starter Stefancom

    (@stefancom)

    Hey Jory @keraweb

    Thank you very much for your comment!

    So, I guess first of all the first part of my tries must go, yes? I base the solution fully on the Get Values from a Custom Relationship Field snippet?

    <del datetime="2020-08-11T15:57:27+00:00"><?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']</del>
    
    <?php 
        	//get Pods object for current post
        	$pod = pods( 'concerts', get_the_id() );
    	//get the value for the relationship field
    	$related = $pod->field( 'composition' );
    	//loop through related field, creating links to their own pages
    	//only if there is anything to loop through
    	if ( ! empty( $related ) ) {
    		count ( $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 )
    ?>

    That would me leave with the code above, yes? I changed the the pod name and the relationship field name to my case.

    But then, WordPress confronts me with an error (Parse error: syntax error, unexpected ‘as’ (T_AS), expecting ‘)’ in code on line 9). Has that to do with $related as $rel, now that I changed foreach to count? That probably is not right?

    Furthermore, this code is to echo a list, right? But, how could I make it echo a mere number instead? With something like echo count($related);?

    Would the $id = $rel[ ‘ID’ ]; bit even be necessary for what I intend, like, maybe I should shorten the code even more?

    Best,

    Stefan

    Thread Starter Stefancom

    (@stefancom)

    Postscriptum: The formatting of the code was not a good idea. Here I paste it again, including the formatting:

    <?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’]

    <?php
    //get Pods object for current post
    $pod = pods( ‘concerts’, get_the_id() );
    //get the value for the relationship field
    $related = $pod->field( ‘composition’ );
    //loop through related field, creating links to their own pages
    //only if there is anything to loop through
    if ( ! empty( $related ) ) {
    count ( $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 ‘‘.get_the_title( $id ).’‘;
    //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 )
    ?>

    Thread Starter Stefancom

    (@stefancom)

    O.K., this too was not a good idea. I hope you can see what my code quote was intended to be about.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @stefancom

    I’m not exactly following your comments at the moment.

    Please read the documentation about count() from PHP:
    https://www.php.net/manual/en/function.count.php

    This is incorrect:
    count ( $related as $rel )

    Count is a function, not a loop or anything:
    $total = count( $related );

    Cheers, Jory

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘“total_pods”, but with a relationship field filter’ is closed to new replies.