Title: Fatal error WP_Query
Last modified: September 5, 2020

---

# Fatal error WP_Query

 *  Resolved [maxcondor](https://wordpress.org/support/users/maxcondor/)
 * (@maxcondor)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/)
 * Hello,
    I put this simple code in a snippet. I get an error that blocks the whole
   site. I can’t log in to the dashboard. I need to log into the database and disable
   the database snippet. Where is it wrong?
 * / The Query
 *     ```
       $args = array(
       	'post_type' => 'post',
       );
       $the_query = new WP_Query( $args );
   
       // The Loop
       if ( $the_query->have_posts() ) {
           echo '<ul>';
           while ( $the_query->have_posts() ) {
               $the_query->the_post();
               echo '<li>' . get_the_title() . '</li>';
           }
           echo '</ul>';
       } else {
           echo '<h1>no posts found</h1>';
       }
       /* Restore original Post Data */
       wp_reset_postdata();
       ```
   
 * Log:
 *     ```
       [05-Sep-2020 13:04:01 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_queried_object_id() on null in /home/customer/www/xxxxxxxxx/public_html/wp-includes/query.php:60
       Stack trace:
       #0 /home/customer/www/xxxxxxxxx/public_html/wp-includes/class-wp-query.php(4337): get_queried_object_id()
       #1 /home/customer/www/xxxxxxxxx/public_html/wp-includes/class-wp-query.php(4273): WP_Query->generate_postdata(Object(WP_Post))
       #2 /home/customer/www/xxxxxxxxx/public_html/wp-includes/class-wp-query.php(3314): WP_Query->setup_postdata(Object(WP_Post))
       #3 /home/customer/www/xxxxxxxxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code(11): WP_Query->the_post()
       #4 /home/customer/www/xxxxxxxxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446): eval()
       #5 /home/customer/www/xxxxxxxxx/public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(534): execute_snippet('// The Query\r\n$...', 15)
       #6 /home/customer/www/xxxxxxxxx/public_html/wp-includes/class-wp-hook.php(287): execute_active in /home/customer/www/xxxxxxxxx/public_html/wp-includes/query.php on line 60
       {excessive log entries redacted}
       ```
   
    -  This topic was modified 5 years, 8 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Moderator [t-p](https://wordpress.org/support/users/t-p/)
 * (@t-p)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13365681)
 * > I put this simple code in a snippet.
 * Where? Which file?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13366100)
 * I recommend asking for help through the code-snippets plugin’s dedicated support
   channel. It’s trying to `eval` your snippet. Using `eval` isn’t always 100% reliable.
 * Alternatives to `eval` is to place your code on a template file, or execute it
   from an action hook, or convert it into a shortcode or custom block.
 *  [fermanus](https://wordpress.org/support/users/fermanus/)
 * (@fermanus)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13367668)
 * Hello,
 * For me this code (unmodified) worked well when wrapped into a function with a
   shortcode and placed either in functions.php or using the plugins “my-custom-
   functions” or “code-snippets”.
 *  Thread Starter [maxcondor](https://wordpress.org/support/users/maxcondor/)
 * (@maxcondor)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13367900)
 * Hello
    I inserted the code using the Code Snippets plugin. the site crashes. 
   I created a child theme of Astra and placed the code in the function.php file.
   It works! The problem is that the query affects all pages on the site including
   the back office. Is there any way to query for a single page only?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13368190)
 * Sure! Depending on whether you have the post’s ID or slug, add one or the other
   to the $args array (substituting valid values of course):
    `'p' => 12345,` or`'
   name' => 'foobar',`
    -  This reply was modified 5 years, 8 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
 *  Thread Starter [maxcondor](https://wordpress.org/support/users/maxcondor/)
 * (@maxcondor)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13371835)
 * ‘name’ => ‘home’,
 * in this way the query will be executed only on the “home” page. is it correct?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13376755)
 * Not quite. Assuming you’ve selected “static” home page in WP settings and chose
   a page named “home” to serve as your home page, then use `pagename' => 'home',`.“
   name” is for single post names.
 *  Thread Starter [maxcondor](https://wordpress.org/support/users/maxcondor/)
 * (@maxcondor)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13377165)
 * Perfect!
    Thanks!
 *  Thread Starter [maxcondor](https://wordpress.org/support/users/maxcondor/)
 * (@maxcondor)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13383490)
 * Ok

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Fatal error WP_Query’ is closed to new replies.

## Tags

 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 9 replies
 * 4 participants
 * Last reply from: [maxcondor](https://wordpress.org/support/users/maxcondor/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/fatal-error-wp_query/#post-13383490)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
