• I have been experimenting with the following standard codex query and loop:

    define('WP_USE_THEMES', false);
    require('../../wp/wp-blog-header.php');
    ....
    <?php $my_query = new WP_Query("showposts=1&post_type=page&page_id=4");
    	while ($my_query->have_posts()) : $my_query->the_post();
    		 $do_not_duplicate = $post->ID; ?>
    			<p>
    			      <?php the_content(); ?>
    			</p>
    	<?php endwhile; ?>

    I have a PHP service where I would like to implement this in a class. e.g.:

    <?php
    define('WP_USE_THEMES', false);
    require_once '../../wp/wp-blog-header.php';
    
    class CMSService {
    	public function getPage() {
    		$my_query = new WP_Query("showposts=1&post_type=page&page_id=4");
                    while ($my_query->have_posts()) {
                         $my_query->the_post();
                         $do_not_duplicate = $post->ID;
                         the_content();
                   }
    	}
    }
    ?>

    I’m not sure of the return type yet, perhaps json_encode, perhaps htmlentities..that is rather trivial as the JQuery request will decode JSON or HTML or whatever I send back.

    The main issue here is that I am receiving a 500 ‘Database Error: Error establishing a database connection’ in the POST request result. Note that in a standard HTML template in the same directory using the loop style from first example does not error. Any ideas or examples you might be able to share?
    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dougrdotnet

    (@dougrdotnet)

    Potentially a moderator might move to the advanced forum (I don’t have write permissions there)?

    My first thought is you need to declare the global $wpquery inside your class method.

    Thread Starter dougrdotnet

    (@dougrdotnet)

    Hi rachelbaker and thanks!
    I thought I was already instantiating $wp_query inside the getPage class method here (this is also where I believe the Database Error is caused):
    $my_query = new WP_Query("showposts=1&post_type=page&page_id=4");
    I am not getting any other errors in logs or in Charles or dev tools that give me any other good error information. It acts as if blog-header doesn’t exist, however, if I change the path in the require i do get an error that it is not found.

    Could you show me an example of what you are thinking?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query in a Service Class’ is closed to new replies.