• Resolved Halit Ayarc?

    (@doktorramiz)


    Hello,

    I have two pods, one is “actors” the other one is “films.
    I established a bi-directional relatiobship between them and it works fine.
    The problem I have is when I try to list the films of the actor in the actor page. I use the code below:

    [if actor_films]					
          <h2> <span class="films_of_title">Below is the list of {@actor_films,count} 
            {@actor_name} films in the database:</span></h2> 
    	
    	<ul class="filmlist">
               [each actor_films]
    		<li><a href="{@permalink,esc_url}">{@name}</a> <br>Film Genre: <span style="font-size:90%;font-weight:400;">{@film_genre}</span></li>
               [/each]
                		
            </ul> 
    	
     [else]
    	<p>This actor has no films in our database.</p>
     [/if] 	

    [each] loop returns the list of films fine but they are sorted by ID not alphabetically. How can I fix this?

    Thanks
    Mehmet

    • This topic was modified 2 years, 6 months ago by Halit Ayarc?.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Hi Mehmet,

    It would have to be written in PHP to sort the films by name.

    Below is example code in the format of a shortcode plugin —?it can be installed as a plugin, or in a theme, or with the Snippets plugin. It uses the Post Title for the actor and film names, but has in comments methods to use meta for names.

    <?php
    /**
     * Plugin Name: Shortcode — actorfilms
     * Description: Adds <code>[actorfilms]</code> shortcode, which can be used on an actor page or template, or called with an actor ID like <code>[actorfilms id="123"]</code>.
     */
    add_shortcode(
    	'actorfilms',
    	function( $a ) {
    		// Actor ID.
    		if ( ! empty( $a['id'] ) ) {
    			$actor_id = $a['id'];
    		}else {
    			$actor_id = get_the_ID();
    		}
    
    		$actor_films = get_post_meta( $actor_id, 'actor_films', false );
    
    		if ( ! empty( $actor_films ) ) {
    			// Sort
    			usort( $actor_films, function( $a, $b ) {
    				// Sort by post title.
    				return strcmp( $a['post_title'], $b['post_title'] );
    				// Alternative: sort by 'name' meta field.
    				// return strcmp( get_post_meta( $a['ID'], 'name', true ), get_post_meta( $b['ID'], 'name', true ) );
    			});
    
    			// Heading
    			printf(
    				'<h2><span class="films_of_title">Below is the list of %d 
    						%s films in the database:</span></h2><ul class="filmlist">',
    				count( $actor_films ),
    				get_the_title( $actor_id ) // or get_post_meta( $actor_id, 'actor_name', true )
    			);
    			// Actor films
    			foreach ( $actor_films as $film ) {
    				printf(
    					'<li><a href="%s">%s</a> <br>Film Genre: <span style="font-size:90%%;font-weight:400;">%s</span></li>',
    					esc_url( get_permalink( $film['ID'] ) ),
    					esc_html( $film['post_title'] ), // or get_post_meta( $film['ID'], 'name', true )
    					esc_html( get_post_meta( $film['ID'], 'film_genre', true ) )
    				);
    			}
    			echo '</ul>';
    		}
    	}
    );
    Thread Starter Halit Ayarc?

    (@doktorramiz)

    Hi Paul,

    Thank you very much. The sorting is done alphabetically now and it works like a charm as a shortcode which I defined through Snippets plugin. The only thing is ‘film_genre’ field is not displaying. It lists the films in correct order and with their correct permalinks but “Film Genre:” stays empty.

    Also when I add the shortcode in the auto template, it doesn’t matter where I put [actorfilms] it always writes on the top section

    Thanks Mehmet

    • This reply was modified 2 years, 6 months ago by Halit Ayarc?.
    Plugin Support Paul Clark

    (@pdclark)

    If film_genre is not showing, verify the meta key matches exactly —?e.g., film_genre not film-genre or filmgenre. Also verify that it’s a simple text field —?a relationship field would need to be coded differently.

    Apologies about the shortcode appearing at the top —?shortcodes should return output, not output directly. Here is the corrected code:

    <?php
    /**
     * Plugin Name: Shortcode — actorfilms
     * Description: Adds <code>[actorfilms]</code> shortcode, which can be used on an actor page or template, or called with an actor ID like <code>[actorfilms id="123"]</code>.
     */
    add_shortcode(
    	'actorfilms',
    	function( $a ) {
    		ob_start();
    		// Actor ID.
    		if ( ! empty( $a['id'] ) ) {
    			$actor_id = $a['id'];
    		}else {
    			$actor_id = get_the_ID();
    		}
    
    		$actor_films = get_post_meta( $actor_id, 'actor_films', false );
    
    		if ( ! empty( $actor_films ) ) {
    			// Sort
    			usort( $actor_films, function( $a, $b ) {
    				// Sort by post title.
    				return strcmp( $a['post_title'], $b['post_title'] );
    				// Alternative: sort by 'name' meta field.
    				// return strcmp( get_post_meta( $a['ID'], 'name', true ), get_post_meta( $b['ID'], 'name', true ) );
    			});
    
    			// Heading
    			printf(
    				'<h2><span class="films_of_title">Below is the list of %d 
    						%s films in the database:</span></h2><ul class="filmlist">',
    				count( $actor_films ),
    				get_the_title( $actor_id ) // or get_post_meta( $actor_id, 'actor_name', true )
    			);
    			// Actor films
    			foreach ( $actor_films as $film ) {
    				printf(
    					'<li><a href="%s">%s</a> <br>Film Genre: <span style="font-size:90%%;font-weight:400;">%s</span></li>',
    					esc_url( get_permalink( $film['ID'] ) ),
    					esc_html( $film['post_title'] ), // or get_post_meta( $film['ID'], 'name', true )
    					esc_html( get_post_meta( $film['ID'], 'film_genre', true ) )
    				);
    			}
    			echo '</ul>';
    		}
    		return ob_get_clean();
    	}
    );
    
    Thread Starter Halit Ayarc?

    (@doktorramiz)

    Hi Paul,

    Thanks for the correction. The location is fixed; but what I forgot to mention earlier was “film_genre” is a custom taxonomy. It’s not a simple text field. I checked the meta key and it exactly matches and it is still not showing…

    Thanks
    Mehmet

    Thread Starter Halit Ayarc?

    (@doktorramiz)

    I tired changing

    esc_html( get_post_meta( $film['ID'], 'film_genre', true ) )

    to

    esc_html( get_term_meta( $term_id['793'], 'film_genre', true ) )

    but still couldn’t get the custom taxonomy data. 793 being the ID of the custom taxonomy “film_genre”

    Plugin Support Paul Clark

    (@pdclark)

    Hi Mehmet,

    If film_genre is a relationship to a taxonomy, then it needs to be treated as an array:

    <?php
    /**
     * Plugin Name: Shortcode — actorfilms
     * Description: Adds <code>[actorfilms]</code> shortcode, which can be used on an actor page or template, or called with an actor ID like <code>[actorfilms id="123"]</code>.
     */
    add_shortcode(
    	'actorfilms',
    	function( $a ) {
    		ob_start();
    		// Actor ID.
    		if ( ! empty( $a['id'] ) ) {
    			$actor_id = $a['id'];
    		}else {
    			$actor_id = get_the_ID();
    		}
    
    		$actor_films = get_post_meta( $actor_id, 'actor_films', false );
    
    		if ( ! empty( $actor_films ) ) {
    			// Sort
    			usort( $actor_films, function( $a, $b ) {
    				// Sort by post title.
    				return strcmp( $a['post_title'], $b['post_title'] );
    				// Alternative: sort by 'name' meta field.
    				// return strcmp( get_post_meta( $a['ID'], 'name', true ), get_post_meta( $b['ID'], 'name', true ) );
    			});
    
    			// Heading
    			printf(
    				'<h2><span class="films_of_title">Below is the list of %d 
    						%s films in the database:</span></h2><ul class="filmlist">',
    				count( $actor_films ),
    				get_the_title( $actor_id ) // or get_post_meta( $actor_id, 'actor_name', true )
    			);
    			// Actor films
    			foreach ( $actor_films as $film ) {
    				printf(
    					'<li><a href="%s">%s</a> <br>Film Genre: <span style="font-size:90%%;font-weight:400;">%s</span></li>',
    					esc_url( get_permalink( $film['ID'] ) ),
    					esc_html( $film['post_title'] ), // or get_post_meta( $film['ID'], 'name', true )
    					esc_html( 
    						implode(
    							', ',
    							wp_list_pluck(
    								get_post_meta( $film['ID'], 'film_genre', false ),
    								'name'
    							)
    						)
    					)
    				);
    			}
    			echo '</ul>';
    		}
    		return ob_get_clean();
    	}
    );
    Thread Starter Halit Ayarc?

    (@doktorramiz)

    Hi Paul,

    I think I’d better drop the film_genre field; because it is still not displaying ??

    Thank you for your time and attention. I really don’t want to trouble you any longer as I already took too much of your time.

    Thank you very much for your help ??

    Mehmet

    Thread Starter Halit Ayarc?

    (@doktorramiz)

    Hi Paul

    I did the lazy thing, and created a simple text field which had the data from the custom taxonomy. I know it’s not an ideal solution but when I need the data as text I’ll use that field and when I need taxonomy I’ll use “film_genre”

    Thank you very much for your help.

    Mehmet

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘each loop list is sorted by document id not alphabetically’ is closed to new replies.