WP_Query in a Service Class
-
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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘WP_Query in a Service Class’ is closed to new replies.