• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

PHP Wizrds…help please!

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › PHP Wizrds…help please!

This topic is: not resolved

Tagged: php

  • This topic has 7 replies, 2 voices, and was last updated 12 years, 11 months ago by Posh John.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • September 3, 2013 at 5:38 am #60390
    Posh John
    Participant

    I am creating a list (will be of my custom taxonomy) and trying to sort the data. My custom taxonomy is a list of names and i want to list by surname.
    I have created a single page template to show my list but i am getting a php error - Missing argument 2 for lastNameSort() - how do i fix this?

    Here is my code so far:

    <?php /*
    Template Name: All Artists
    */ ?> 
    
    <?php
    
    add_action( 'genesis_entry_content', 'lastNameSort');
    function lastNameSort($a, $b ) {
        $aLast = end(explode(' ', $a));
        $bLast = end(explode(' ', $b));
    
        return strcasecmp($aLast, $bLast);
    
    // The array of data (will remove this and use the custom taxonomy once i know it works).
    $array = array(
        27 => 'Sarah Green',
        29 => 'Adam Brown',
        68 => 'Fred Able'
    );
    
    uasort($array, 'lastNameSort');
    
    print_r($array);
    
    }
    
    genesis();

    Thanks for your help 🙂


    Thus the heavens and the earth were completed in all their vast array. Genesis 2

    September 3, 2013 at 6:26 am #60396
    Gary Jones
    Member

    Change:

    add_action( 'genesis_entry_content', 'lastNameSort');

    to:

    add_action( 'genesis_entry_content', 'lastNameSort', 10, 2 );


    WordPress Engineer, and key contributor the Genesis Framework | @GaryJ

    September 3, 2013 at 6:37 am #60399
    Posh John
    Participant

    Hi Gary,

    Thanks for stopping by. I am still getting the error though. Its says:
    Warning: Missing argument 2 for lastNameSort() in /home/editions/public_html/wp-content/themes/mytheme-genesis/single-artists.php on line 8
    Line 8 is:

    function lastNameSort($a, $b ) {

    Any more ideas are welcome 🙂

    Thanks


    Thus the heavens and the earth were completed in all their vast array. Genesis 2

    September 3, 2013 at 8:53 am #60424
    Gary Jones
    Member

    You've got a callback for lastNameSort inside of the definition of lastNameSort. Try moving the } just near the end, to just before the // line.


    WordPress Engineer, and key contributor the Genesis Framework | @GaryJ

    September 3, 2013 at 1:56 pm #60487
    Posh John
    Participant

    Hi Gary,

    Firstly, thanks for your help so far.

    I've been at this all day now and i have finally reached a eureka moment...and i'm as happy as a pig in sh*t, but, there's one more piece to the puzzle.

    i now have my taxonomy listing in surname order but i can't figure out how to make the names link to the correct pages. When i query WP i am only asking for the taxonomy name (otherwise my name-splitter function throws an error). do you have any ideas how i can query the taxonomy and have the results link to their pages?

    Here is what i have so far...

    <?php /*
    Template Name: All Artists
    */ ?> 
    
    <?php
    
    function namesplitter($a, $b) {
        $aLast = end(explode(' ', $a));
        $bLast = end(explode(' ', $b));
    
        return strcasecmp($aLast, $bLast);
    }
    
    add_action( 'genesis_entry_content', 'display_sorted_tax', 10, 2 );
    function display_sorted_tax() {
    
    $args = array(
    	'taxonomy' => 'artist',
    	'fields' => 'names'
    	);
    
    $terms = get_terms('artist', $args);
    uasort($terms, 'namesplitter');
    
    $count = count($terms); $i=0;
    if ($count > 0) {
        $term_list = '<p class="my_term-archive">';
        foreach ($terms as $term) {
            $i++;
        	$term_list .= $term;
        	if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '</p>';
        }
        echo $term_list;
    }
    
    }
    
    genesis();

    Thus the heavens and the earth were completed in all their vast array. Genesis 2

    September 3, 2013 at 2:56 pm #60497
    Posh John
    Participant

    Hi again Gary,

    Don't worry i've figured it out (although there may probably be a better way!!). Anyway, i added a get_term_by to fetch the details i needed. I know that makes a lot of queries, but it's the best i could figure out with my limited php ability.

    See what you think:

    <?php /*
    Template Name: All Artists
    */ ?> 
    
    <?php
    
    function namesplitter($a, $b) {
        $aLast = end(explode(' ', $a));
        $bLast = end(explode(' ', $b));
    
        return strcasecmp($aLast, $bLast);
    }
    
    add_action( 'genesis_entry_content', 'display_sorted_tax', 10, 2 );
    function display_sorted_tax() {
    
    $args = array(
    	'taxonomy' => 'artist',
    	'fields' => 'names'
    	);
    
    $terms = get_terms('artist', $args);
    uasort($terms, 'namesplitter');
    
    $count = count($terms); $i=0;
    if ($count > 0) {
        $term_list = '<p class="my_term-archive">';
        foreach ($terms as $termname) {
    		$term = get_term_by('name', $termname, 'artist');
            $i++;
        	$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
        	if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '</p>';
    	}
        echo $term_list;
    }
    
    }
    
    genesis();

    Thus the heavens and the earth were completed in all their vast array. Genesis 2

    September 3, 2013 at 6:38 pm #60536
    Gary Jones
    Member
    &lt;?php
    /*
     * Template Name: All Artists
     */
    
    add_action( 'genesis_entry_content', 'poshjohn_display_artists' );
    /**
     * Display linked artists, ordered by surname.	
     *
     * @return null Return early if there are no artists.
     */
    function poshjohn_display_artists() {
    	$terms = poshjohn_get_sorted_artists();
    
    	// If there's no artists, abort now.
    	if ( ! $terms ) {
    		return;
    	}
    
    	$count = count( $terms );
    
    	$term_list = '&lt;p class=&quot;my_term-archive&quot;&gt;';
    	foreach ( $terms as $index =&gt; $termname ) {
    		$term = get_term_by( 'name', $termname, 'artist' );
    		$term_list = sprintf(
    			'<a href="&quot;%s&quot;" title="&quot;%s&quot;">%s</a>',
    			esc_url( get_term_link( $term ) ),
    			esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term-&gt;name ) ),
    			$term-&gt;name
    		);
    
    		if ( $index &lt; $count )
    			$term_list .= ' &middot; ';
    		else
    			$term_list .= '&lt;/p&gt;';
    	}
    	
    	echo $term_list;
    }
    
    /**
     * Get the list of artists and sort them.
     *
     * @return array. List of artists, sorted by surname. May be empty array of no artists found or taxonomy does not exist.
     */
    function poshjohn_get_sorted_artists() {
    	$args = array(
    		'taxonomy' =&gt; 'artist',
    		'fields'   =&gt; 'names',
    	);
    
    	$terms = get_terms( 'artist', $args );
    
    	// If artist taxonomy does not exist, play nice with functions that expect an array.
    	if ( is_wp_error( $terms ) )
    		return array();
    
    	// Sort names by surname
    	uasort( $terms, 'poshjohn_sort_by_surname_callback' );
    
    	return $terms;
    }
    
    /**
     * Callback for array sorting.
     *
     * For $a and $b, we split the string by a space, then take the last portion of each, and do a case insensitive
     * binary-safe string comparison.
     *
     * @param  string $a
     * @param  string $b
     *
     * @return string Return &lt; 0 if $a is less than $b; &gt; 0 if $a is greater than $b, and 0 if they are equal.
     */
    function poshjohn_sort_by_surname_callback( $a, $b ) {
    	$aLast = end( explode( ' ', $a ) );
    	$bLast = end( explode( ' ', $b ) );
    
    	return strcasecmp( $aLast, $bLast );
    }
    
    genesis();
    

    That's how I would do it - minor refactoring to make the code easier to read, follow code standards, and have functions do only one task if possible. You might need to adjust that conditional that adds the middot, with a +1 added on to $count or something like that.


    WordPress Engineer, and key contributor the Genesis Framework | @GaryJ

    September 4, 2013 at 4:36 am #60585
    Posh John
    Participant

    Hi Gary,

    Thanks for that Gary. Even though i got it to work, it's fantastic to see how it is done "properly", so i really appreciate the time you took to show me this.

    I will now study your code and go off and research the terms you used to try and learn why you did it the way you did...this is invaluable to me learning to code better.

    Thanks again for taking the time to provide this.

    Kind regards
    John.


    Thus the heavens and the earth were completed in all their vast array. Genesis 2

  • Author
    Posts
Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble