• I’m running into a problem with my WP site and hoping somebody knows a bit more about PHP then me.

    What I’m trying to achieve is changing the ordering of posts. The website https://oahn.ca/networks/small-ruminants/#reportsLink

    It is now ordered (from what I can see in the PHP) with the ‘slug’ ordering. But I want all the reports to show in ‘desc’ order by date.
    Below is the code I’m editing. I’m just getting started with PHP and after a ton of forums my options running out.

    <div class="container main">
    
     <?php
    
        foreach ( $report_quarters as $report_quarter ) {
    
    		if ($report_quarter->parent != 0) { 
    
    if (have_posts($report_quarters)) {
    	echo "<div id='".$report_quarter->slug."-container' class='show-div'>";
    
    } else {
    
    }
    
    	$myposts = get_posts(array(
      	'numberposts' => -1,
      	'orderby' => 'post_date',
    	'order' => 'DESC',
        'post_type' => array('reports'),
    	'post_status' => $post_status,
        'tax_query' => array(
    
            array(
            'taxonomy' => 'network',
            'field' => 'slug',
            'terms' => array($term->slug)
    		),
    
    		 array(
            'taxonomy' => 'report-quarter',
    
            'field' => 'slug',
            'terms' => array($report_quarter->slug)
    		)
    
        ))
    );
    
    if (empty($myposts)) {
     //$placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1>No reports available for this quarter.</div><div class='clearfix'></div>";
     //echo $placeholder;	
    
    } else {
    	unset($placeholder);
     $placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1></div><div class='clearfix'></div>";
     echo $placeholder;
    }
    
    foreach ($myposts as $mypost) {
    
    	global $mypost;
    
    		$args = array(
    		'numberposts' => -1,
    		'order' => 'ASC',
    		'post_parent' => $mypost->ID,
    		'post_type' => 'attachment'
    	);
    
    	$attachments = get_children( $args );
    
    	echo "<div class='col-sm-3 no-padding'>
    	<div class='single-report'>
    	".apply_filters('the_content', $mypost -> post_content)."</div>";
    
    		foreach ($attachments as $attachment) {
    echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank"  class="report-title">'.$mypost->post_title.'</a></div>';
    
    }
    
     }
    
     echo "<div class='clearfix'></div>";
    
     	echo "</div>";
    
        }}
    
    ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter blackbike

    (@blackbike)

    to clarify, the order is now:

    • Q4 2015
    • Q4 2014
    • Q3 2015
    • Q3 2014
    • Q2 2015
    • Q2 2014

    …etc etc.

    And it should be order like this:

    • Q4 2015
    • Q3 2015
    • Q2 2015
    • Q4 2014
    • Q3 2014
    • Q2 2014

    Anybody any ideas where to edit this?

    Thread Starter blackbike

    (@blackbike)

    I’ve tried that but didn’t work unfortunately/

    ok it is working, but they are in order and categorized per quarter.
    seems maybe the year loop is inside a category loop.

    foreach ( $report_quarters as $report_quarter )

    try moving year loop above the category loop.

    Thread Starter blackbike

    (@blackbike)

    Hi Christian,

    Thanks for your reply, I’ve been trying to edit the 2 loops but I’m not PHP savvy enough to make this work.
    I see what you mean that there are two loops but how to switch them is not working for me yet.

    Could you point me in the correct correct, that would be great!!!

    Cheers

    try this, remove

    foreach ( $report_quarters as $report_quarter ){

    and the last
    }

    Thread Starter blackbike

    (@blackbike)

    sorry for my inability to make this happen, The loop looks like this, could you maybe replace the this code with the correct one that would be great!

    <?php
    
        foreach ( $report_quarters as $report_quarter ) {
    
    		if ($report_quarter->parent != 0) { 
    
    if (have_posts($report_quarters)) {
    	echo "<div id='".$report_quarter->slug."-container' class='show-div'>";
    
    } else {
    
    }
    
    	$myposts = get_posts(array(
      	'numberposts' => -1,
      	'orderby' => 'post_date',
    	'order' => 'DESC',
        'post_type' => array('reports'),
    	'post_status' => $post_status,
        'tax_query' => array(
    
            array(
            'taxonomy' => 'network',
            'field' => 'slug',
            'terms' => array($term->slug)
    		),
    
    		 array(
            'taxonomy' => 'report-quarter',
    
            'field' => 'slug',
            'terms' => array($report_quarter->slug)
    		)
    
        ))
    );
    
    if (empty($myposts)) {
     //$placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1>No reports available for this quarter.</div><div class='clearfix'></div>";
     //echo $placeholder;	
    
    } else {
    	unset($placeholder);
     $placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1></div><div class='clearfix'></div>";
     echo $placeholder;
    }
    
    foreach ($myposts as $mypost) {
    
    	global $mypost;
    
    		$args = array(
    		'numberposts' => -1,
    		'order' => 'DESC',
    		'orderby' => 'name',
    		'post_parent' => $mypost->ID,
    		'post_type' => 'attachment'
    	);
    
    	$attachments = get_children( $args );
    
    	echo "<div class='col-sm-3 no-padding'>
    	<div class='single-report'>
    	".apply_filters('the_content', $mypost -> post_content)."</div>";
    
    		foreach ($attachments as $attachment) {
    echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank"  class="report-title">'.$mypost->post_title.'</a></div>';
    
    }
    
     echo "<div class='clearfix'></div>";
    
     	echo "</div>";
    
        }}
    
    ?>
    <div class="container main">
    
     <?php
    
    		if ($report_quarter->parent != 0) { 
    
    if (have_posts($report_quarters)) {
    	echo "<div id='".$report_quarter->slug."-container' class='show-div'>";
    
    } else {
    
    }
    
    	$myposts = get_posts(array(
      	'numberposts' => -1,
      	'orderby' => 'post_date',
    	'order' => 'DESC',
        'post_type' => array('reports'),
    	'post_status' => $post_status,
        'tax_query' => array(
    
            array(
            'taxonomy' => 'network',
            'field' => 'slug',
            'terms' => array($term->slug)
    		),
    
    		 array(
            'taxonomy' => 'report-quarter',
    
            'field' => 'slug',
            'terms' => array($report_quarter->slug)
    		)
    
        ))
    );
    
    if (empty($myposts)) {
     //$placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1>No reports available for this quarter.</div><div class='clearfix'></div>";
     //echo $placeholder;	
    
    } else {
    	unset($placeholder);
     $placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1></div><div class='clearfix'></div>";
     echo $placeholder;
    }
    
    foreach ($myposts as $mypost) {
    
    	global $mypost;
    
    		$args = array(
    		'numberposts' => -1,
    		'order' => 'ASC',
    		'post_parent' => $mypost->ID,
    		'post_type' => 'attachment'
    	);
    
    	$attachments = get_children( $args );
    
    	echo "<div class='col-sm-3 no-padding'>
    	<div class='single-report'>
    	".apply_filters('the_content', $mypost -> post_content)."</div>";
    
    		foreach ($attachments as $attachment) {
    echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank"  class="report-title">'.$mypost->post_title.'</a></div>';
    
    }
    
     }
    
     echo "<div class='clearfix'></div>";
    
     	echo "</div>";
    
        }
    
    ?>
    Thread Starter blackbike

    (@blackbike)

    Thanks for the snippet! Unfortunately this is not working. When I replace my snippet with yours all reports disapear in total…

    Any other ideas would be every helpful, cheers!

    ok try

    <div class="container main">
    
     <?php
    
    if (have_posts($report_quarters)) {
    	echo "<div id='".$report_quarter->slug."-container' class='show-div'>";
    
    } else {
    
    }
    
    	$myposts = get_posts(array(
      	'numberposts' => -1,
      	'orderby' => 'post_date',
    	'order' => 'DESC',
        'post_type' => array('reports'),
    	'post_status' => $post_status,
        'tax_query' => array(
    
            array(
            'taxonomy' => 'network',
            'field' => 'slug',
            'terms' => array($term->slug)
    		),
    
    		 array(
            'taxonomy' => 'report-quarter',
    
            'field' => 'slug',
            'terms' => array($report_quarter->slug)
    		)
    
        ))
    );
    
    if (empty($myposts)) {
     //$placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1>No reports available for this quarter.</div><div class='clearfix'></div>";
     //echo $placeholder;	
    
    } else {
    	unset($placeholder);
     $placeholder = "<div class='col-md-12'><h1>".$report_quarter->name."</h1></div><div class='clearfix'></div>";
     echo $placeholder;
    }
    
    foreach ($myposts as $mypost) {
    
    	global $mypost;
    
    		$args = array(
    		'numberposts' => -1,
    		'order' => 'ASC',
    		'post_parent' => $mypost->ID,
    		'post_type' => 'attachment'
    	);
    
    	$attachments = get_children( $args );
    
    	echo "<div class='col-sm-3 no-padding'>
    	<div class='single-report'>
    	".apply_filters('the_content', $mypost -> post_content)."</div>";
    
    		foreach ($attachments as $attachment) {
    echo '<a href="' . wp_get_attachment_url( $attachment->ID ) . '" target="_blank"  class="report-title">'.$mypost->post_title.'</a></div>';
    
    }
    
     }
    
     echo "<div class='clearfix'></div>";
    
     	echo "</div>";
    
    ?>
    Thread Starter blackbike

    (@blackbike)

    unfortunately same result and all reports disapear =( I see what you tried to do tho so that’s a nice thing to learn on the PHP side.

    GL

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘taxonomy and order of posts’ is closed to new replies.