Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t know if I’m smarter, maybe more experienced with WP at least ??

    I’m not familiar with the SportsPress plugin. I don’t see how a player’s birth date day has anything to do with the date a post is published. I imagine a player is maybe managed as a custom post type? Or is it as a user? Are we even querying for players or something else?

    In any case, you are limited to the values specified in the WP_Query docs for the orderby argument, it’s not possible to define other values when instantiating the class object. However, you can customize the SQL order by clause through the “posts_orderby” filter. Here you can alter the clause in any way desired, as long as it’s valid mySQL.

    The question remains, how to sort by day number. The published date field is a UNIX timestamp, so some sort of SQL function would be required to extract the day number from the timestamp. DAYOFMONTH() should work, but I’m weak with SQL functions. It works for date strings, I don’t know about timestamps. If that doesn’t work, FROM_UNIXTIME() should work, it’s like PHP’s date() function, except the parameter order is swapped.

    If the published date in fact has no relationship to player birthdays, then is that what you still want to order by? If the date day is not related to the published date, then storing the date day in post meta does make sense. You could then sort by meta_value_num if no other meta values are involved in the query. But if the date day is derived from the published date or another existing date field, replicating a derivative value is not a great idea. You need to avoid any sort of redundant data, because you then have to be sure the two are always correlated. It’s generally better to dynamically derive values on the fly than to statically derive values in meta data.

    Thread Starter thenicnic

    (@thenicnic)

    Dear bcworkz,

    thank you for this very detailed answer! Your hint with the post publish date was great. Indeed this seems to be a custom post type with the publishing date as birthday. This is my final source code of the sportspress/templates/birthdays.php page – for solving my problem I just added a sort function which sorts by each player’s birthday date reduced to the actual day of month:

    <?php
    /**
     * Birthdays
     *
     * @author 		ThemeBoy
     * @package 	SportsPress_Birthdays
     * @version     1.9.19
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    $html5 = current_theme_supports( 'html5', 'gallery' );
    $defaults = array(
    	'date' => 'day',
    	'itemtag' => 'dl',
    	'icontag' => 'dt',
    	'captiontag' => 'dd',
    	'size' => 'sportspress-fit-medium',
    	'show_player_birthday' => get_option( 'sportspress_player_show_birthday', 'no' ) == 'yes' ? true : false,
    	'show_staff_birthday' => get_option( 'sportspress_staff_show_birthday', 'no' ) == 'yes' ? true : false,
    	'link_players' => get_option( 'sportspress_link_players', 'yes' ) == 'yes' ? true : false,
    	'link_staff' => get_option( 'sportspress_link_staff', 'yes' ) == 'yes' ? true : false,
    );
    
    extract( $defaults, EXTR_SKIP );
    
    $args = array(
    	'post_type' => array( 'sp_player', 'sp_staff' ),
    	'numberposts' => -1,
    	'posts_per_page' => -1,
    	'orderby' => 'date',
    	'order' => 'ASC',
    	'monthnum' => date('n'),
    );
    
    if ( $date == 'day' ) {
    	$args['day'] = date('j');
    }
    
    $posts = get_posts( $args );
    
    <strong> // The new sort part
    
    // THE SORT FUNCTION
    function sort_by_day_of_month($a, $b) {
      $a_postdate = get_the_date( "d" , $a->ID);
      $b_postdate = get_the_date( "d" , $b->ID);
      return $a_postdate - $b_postdate;
    }
    
    usort($posts, 'sort_by_day_of_month');</strong>
    
    // FILTERS YEARS
    $minimal_year = (date("Y") - 100);
    
    foreach ( $posts as $post ) {
    <strong>	// The year filter part
    $year_of_birth = get_the_date( "Y" , $post->ID );
    	if ( $year_of_birth <= $minimal_year )
    	{
    		continue;
    	}</strong>
     
    	echo '<div class="sp-template sp-template-birthdays sp-template-birthday-gallery sp-template-gallery">';
    
    	if ( 'sp_staff' == $post->post_type ) {
    		$link_posts = $link_staff;
    		$show_birthday = $show_staff_birthday;
    	} else {
    		$link_posts = $link_players;
    		$show_birthday = $show_player_birthday;
    	}
    	
    	$birthday = get_the_date( "j. F" , $post->ID );
    		
    	if ( $show_birthday && $birthday && $group !== $birthday ) {
    		echo '<h4 class="sp-table-caption">' . $birthday . '</h4>';
    	}
    
    	echo '<div class="gallery">';
    
    	$caption = $post->post_title;
    	$caption = trim( $caption );
    
        sp_get_template( 'player-gallery-thumbnail.php', array(
        	'id' => $post->ID,
        	'itemtag' => $itemtag,
        	'icontag' => $icontag,
        	'captiontag' => $captiontag,
        	'caption' => $caption,
        	'size' => $size,
        	'link_posts' => $link_posts,
        ) );
    
    	echo '<br style="clear: both;" />';	
    	echo "</div></div>\n";
    }

    The other capitalized commented if sequence skips players with a year of birth smaller or equals 100 years ago. This seems to be mean to people who are more than 100 years old but they don’t have any of them in their teams. Unfortunately it seems that the smallest year of a post’s date is 115 years ago. I misuse these old ages to hide players with unknown birthday because there isn’t another possibility for that.

    Maybe my code is useful to another one.

    Thank you again and best regards

    Nico

    • This reply was modified 7 years ago by thenicnic.
    • This reply was modified 7 years ago by thenicnic.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘‘orderby’ day in WPQuery’ is closed to new replies.