Support » Plugins » Hacks » Error handling in custom wp_query script

  • Resolved JiL

    (@rwa66)


    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 69

    I 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)
  • Hi, @rwa66. Try changing this code:

    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>';
    }
    

    to look like this instead:

    if (is_array($tosortposts) {
    	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>';
    	}
    }
    

    and see if that resolves the problem or not.

    Thanks for your help, girlieworks!
    If i replace the code it gives me
    Parse error: syntax error, unexpected ';' in page-home.php on line 69
    If i delete the ; on that line it gives me
    Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in page-home.php on line 70

    Oops, that’s my fault, I left out a closing parenthesis in the IF statement, so just add one so it looks like this:

    if (is_array($tosortposts)) {

    • This reply was modified 3 years, 6 months ago by girlieworks.

    Girlieworks, you’re the best!
    It is working as expected.
    Now i will dive into the code and try to learn from it.
    Thanks a lot!

    You’re welcome!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Error handling in custom wp_query script’ is closed to new replies.