WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need Help With WP_Query (sort order) (5 posts)

  1. Justin Case
    Member
    Posted 7 months ago #

    I keep killing my site when trying to modify the arguments in a WP_Query

    If I try to add a sort order such as:

    'orderby' => 'title'

    To the arguments, then the site crashes.

    Here is how I am passing the arguments:

    $args = array(
            'post_parent' => $post->ID,
            'post_type' => 'page'
            'orderby' => 'title'
        );
        $subpages = new WP_query($args);

    But if I leave it OUT of the arguments, then It won't crash. For example, it works fine if I just do this:

    $args = array(
            'post_parent' => $post->ID,
            'post_type' => 'page'
        );
        $subpages = new WP_query($args);

    But once I add 'orderby' => 'title', the site and the admin crash (just a white screen, no source is output to the screen at all).

    And yes, I am trying to return pages, not posts.

    I really am not sure what is going wrong.

    If it helps, I am trying to use this whole bit of code for displaying child pages and their descriptions. The entire bit of code is:

    function subpage_peek() {
        global $post;  
    
        //query subpages
        $args = array(
            'post_parent' => $post->ID,
            'post_type' => 'page'
        );
        $subpages = new WP_query($args);  
    
        // create output
        if ($subpages->have_posts()) :
            $output = '<ul>';
            while ($subpages->have_posts()) : $subpages->the_post();
                $output .= '<li><strong><a href="'.get_permalink().'">'.get_the_title().'</a></strong>
                            <p>'.get_the_excerpt().'<br />
                            <a href="'.get_permalink().'">Continue Reading ?</a></p></li>';
            endwhile;
            $output .= '</ul>';
        else :
            $output = '<p>No subpages found.</p>';
        endif;  
    
        // reset the query
        wp_reset_postdata();  
    
        // return something
        return $output;
    }
    
    add_shortcode('subpage_peek', 'subpage_peek');
    
    // displays category descriptions as well as names - call with desc_cats()
    
    function desc_cats(){
    foreach (get_categories(array('hide_empty'=>true)) as $category)
    {
    $catid = $category->cat_ID;
    echo '<li><a href="' . get_bloginfo('wpurl') . '/' .  $category->category_nicename . '/">' .
    	$category->cat_name . ' (' . $category->count.$numposts.')</a>' . category_description($catid) . '</li>';
    }
    echo'<p>'; // puts a paragraph tag after last category description listed
    };
  2. Josh (WP Edit)
    Volunteer Moderator
    Posted 7 months ago #

    Your syntax is incorrect in the first block of code you posted:

    $args = array(
            'post_parent' => $post->ID,
            'post_type' => 'page'
            'orderby' => 'title'
        );
        $subpages = new WP_query($args);

    Whenever you are passing arguments, they MUST be comma-separated EXCEPT for the last argument.

    Here is the correct syntax:

    $args = array(
        'post_parent' => $post->ID,
        'post_type' => 'page', // <- Note the comma
        'orderby' => 'title'
    );
    $subpages = new WP_query($args);
  3. Justin Case
    Member
    Posted 7 months ago #

    Thanks so much, Josh!!!

    Really appreciate the assist. Don't know how I overlooked the missing comma.

    ~~~

    By the way: Is there a way for a moderator to mark this thread as resolved? I don't seem to be able to do that here.

  4. Josh (WP Edit)
    Volunteer Moderator
    Posted 7 months ago #

    My pleasure.

    Yeah, mods can mark as resolved.

    But so can the original poster (OP in WordPress jargon).

    Look over at the right of the screen where it says "About this topic". Under there should be a dropdown where you can select the status of the topic.

    Choose "resolved" and click update.

  5. Justin Case
    Member
    Posted 7 months ago #

    Thanks again.

    Don't know how I missed it. Been working too long on this project that I am overlooking some of the obvious things.

Reply

You must log in to post.

About this Topic