• Resolved maxcondor

    (@maxcondor)


    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 4 years, 2 months ago by bcworkz.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator t-p

    (@t-p)

    I put this simple code in a snippet.

    Where? Which file?

    Moderator bcworkz

    (@bcworkz)

    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.

    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

    (@maxcondor)

    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

    (@bcworkz)

    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 4 years, 2 months ago by bcworkz.
    Thread Starter maxcondor

    (@maxcondor)

    ‘name’ => ‘home’,

    in this way the query will be executed only on the “home” page. is it correct?

    Moderator bcworkz

    (@bcworkz)

    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

    (@maxcondor)

    Perfect!
    Thanks!

    Thread Starter maxcondor

    (@maxcondor)

    Ok

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Fatal error WP_Query’ is closed to new replies.