Error handling in custom wp_query script
-
I found this script somewhere that connects to another WP-instance and gets 1 post with a certain tag.
?php global $switched; switch_to_blog(1); ?> <?php $query = new WP_Query (array( 'post_type' => array( 'events'), 'tag' => 'gezondheidszorg', 'showposts' => '1', 'order' => 'DESC')); $months = array("", "januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"); function sortFunction( $a, $b ) { return $a["date"] - $b["date"]; } while ( $query->have_posts() ) : $query->the_post(); $date = get_post_meta(get_the_ID(), 'date'); $id = get_the_ID(); $tosortposts []= array( 'date' => strtotime($date[0]), 'link' => get_permalink($id), 'title' => get_the_title($id) ); endwhile; usort($tosortposts, "sortFunction"); foreach($tosortposts as $p){ echo '<strong>Onze volgende workshop:</strong> <a target="_blank" href="'; echo $p['link']; echo '">'; echo date('j ', $p['date']).$months[date("n", $p['date'])].' - '; echo ''; echo $p['title']; echo '</a><p></p><p>'; echo '</p>'; } ?> </div> <?php restore_current_blog(); ?>This script is working just fine as long there is an item with that tag.
But when there is no item with that tag i get these errors:Warning: usort() expects parameter 1 to be array, null given in page-home.php on line 68 Warning: Invalid argument supplied for foreach() in page-home.php on line 69I guess i have to make some sort of error handling, but I am not sure where to start.
Anyone can lend help on this one?
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
The topic ‘Error handling in custom wp_query script’ is closed to new replies.