Support » Fixing WordPress » WP_Query() works but ignores arguments, and WP_DEBUG doesn’t do anything

  • Resolved metasense

    (@metasense)


    I am pretty new to WordPress, so perhaps I am overlooking something dumb. I have a custom page set up called Contents that lists all my posts. Works great. Text shown at end of this email. The problem is I need to change the orderby. I have tried every parameter under the sun, and any parameters to WP_Query() are always ignored, and no errors are reported.

    I tried turning on debugging in my wp-config.php as shown below, but nothing happens, no errors or debug log produced. I am putting these files in the right places on my server and other changes are reflected fine, but parameters to WP_Query are ignored. Can you see what I am doing wrong? I tried remove_all_filters() on a recommendation; it had no effect. I have read the codex over and over and I can’t figure it out. Thanks!

    Steve

    enabling debugging doesn’t work:

    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    define( 'SCRIPT_DEBUG', true );

    my page:

    <?php 
    /*
        Template Name: Contents
    */
    ?>
    <?php get_header(); ?>
    
        <article>
    
            <?php 
            $paged = 1;
            // remove_all_filters('posts_orderby');
            $args = array('orderby' => 'rand');         // always ignored no matter what parameters I give
            $wp_query = new WP_Query( $args );
            $wp_query->query('showposts=500' . '&paged='.$paged);
            while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
            <h3><a>" title="Read more"><?php the_title(); ?></a></h2>
    
            <?php endwhile; ?>
    
            <?php wp_reset_postdata(); ?>
    
        </article>
    
    <?php get_footer(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator t-p

    (@t-p)

    you seem to be setting the ‘orderby’, but then you seem to be creating a totally new query without the ‘orderby’;

    try to combine all parameters into one query:

    <?php 
            $paged = 1;
            $args = array('orderby' => 'rand', 'posts_per_page' => 500, 'paged' => $paged);
    );         
            $wp_query = new WP_Query( $args );
            while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    is there a purpose that you have a fixed $paged?

    Thread Starter metasense

    (@metasense)

    Michael,

    Yes, exactly, that was the explanation, thanks! I didn’t realize that call to query() would override what the constructor did… I guess it seems obvious they are doing the same thing different ways in retrospect, but the differences in syntax and argument names threw me and I didn’t connect them. I borrowed this code which had that query() call already, and so when I wanted to use orderby I used the constructor as the WP_Query doc said, not realizing that the query() would supersede it. I can see now that posts_per_page replaced showposts in 2.1. The $paged was fixed to 1 only because the original code had the ‘&paged=’.$paged notation but I noticed $paged was not set, and so I set it thinking that might help. Looking at the doc, it seems like I shouldn’t use paged at all, I don’t want any paginating.

    I don’t suppose you have any insights as to why I am not seeing any debugging info?

    Thanks again!

    Steve

    Thread Starter metasense

    (@metasense)

    FYI, I am seeing debug info now and the debug log. I guess I hadn’t triggered a PHP error yet, so it didn’t report anything, but a typo did trigger it. So this one is fully resolved, thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_Query() works but ignores arguments, and WP_DEBUG doesn’t do anything’ is closed to new replies.