500 internal server error when try to include wp-load.php in codeigniter site

I have setup a WordPress blog at www.example.com/blog and a Codeigniter site at www.example.com. I am trying to display recent post from blog to my Codeigniter site. To do it, I included the wp-load.php file using

require($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');

   // require_once('../app/blog/wp-load.php');
                         $args = array(
                              // 'cat' => 3, // Only source posts from a specific category
                              'posts_per_page' => 4 // Specify how many posts you'd like to display
                              );
                              $latest_posts = new WP_Query( $args );  
                              if ( $latest_posts->have_posts() ) {
                                while ( $latest_posts->have_posts() ) {
                                $latest_posts->the_post(); ?>
                             <marquee align="top" behavior="scroll" onmouseover="this.stop();" onmouseout="this.start();" direction="up" scrolldelay="200" ><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> 
                            <li>

                              <?php if ( has_post_thumbnail() ) { ?>
                                <span class="post_thumbnail"><?php the_post_thumbnail(); ?></span>
                              <?php } ?>
                              <span class="post_title"><?php the_title(); ?></span>
                            </a>
                            <!--span class="post_time">Posted on <?php the_time('l jS F, Y') ?></span-->
                            <?php //the_excerpt(); ?>
                          </li> 
                          </marquee >
                        <? } 
                            } else {
                            echo '<p>There are no posts available</p>';
                          }
                          wp_reset_postdata(); 
                        ?>

but throws an internal server error.
Do I have to change the file permission? Please help. The site is hosted by Godaddy.

Read More

Error detail :

Internal Server Error

The server encountered an internal error or misconfiguration and was
unable to complete your request.

Please contact the server administrator to inform of the time the
error occurred and of anything you might have done that may have
caused the error.

More information about this error may be available in the server error
log.

Related posts

1 comment

  1. Never include any WordPress file in your main website as it can lead to many errors. Instead, use Codeigniter’s second database connection to connect to your WordPress database.
    Searching for get wordpress posts from database to codeigniter I have found this gist and this article. The following code is taken from this article. Note that this code is for Codeigniter 2 and the code is not tested. But I have dealt with many databases in Codeigniter before, just ask if you find any trouble.

    a. add a different settings data in your application/config/database.php file along side with your main website database settings:

    $db['wordpress']['hostname'] = "wordpress_db_host"; // database host
    $db['wordpress']['username'] = "wordpress_db_username"; // db user name
    $db['wordpress']['password'] = "wordpress_db_password"; // db user password
    $db['wordpress']['database'] = "wordpress_db"; // database name
    

    b. build a WordPress model in application/models/wordpress_model.php:

    <?php if (!defined('BASEPATH')) die ('No direct script access allowed!');
    
    class WordPress_Model extends CI_Model
    {
        function getPosts()
        {
            $this->load->database('wordpress', TRUE); // load database
    
            // get the wordpress posts, customize it as you like
            $this->wordpress->select("post_id, post_title, post_content");
            $this->wordpress->from('post_tbl');
            $this->wordpress->limit(5);
            $query = $this->wordpress->get();
            $data['posts'] = $query->result();
    
            $this->load->view('wordpress_posts_view', $data); // load the view file , we are passing $data array to view file
        }
    }
    

    c. create the view file in application/views/wordpress_posts_view.php:

    <h4>Display Records From Database Using Codeigniter</h4>
    <table>
        <tr>
            <td><strong>Post Id</strong></td>
            <td><strong>Post Title</strong></td>
        </tr>
        <?php foreach($posts as $post){?>
            <tr>
                <td><?php echo $post->post_id;?></td>
                <td><?php echo $post->post_title;?></td>
            </tr>    
        <?php }?>  
    </table>
    

    d. now, you can call all this in any Codeigniter controller:

    <?php if (!defined('BASEPATH')) die ('No direct script access allowed!');
    
    class Homepage extends CI_Controller
    {
        public function index()
        {
            $this->load->model('wordpress_model');
    
            $data['wordpress_html_posts'] = $this->wordpress_model->getPosts();
            print_r($data['wordpress_html_posts']);
        }
    }
    

Comments are closed.