• I have two WordPress blogs on the same domain/server – one regular posts the other videos – plus other non-Wordpress pages which have a box with the latest blog posts using this:

    <?php
    require($_SERVER[‘DOCUMENT_ROOT’] . ‘/blog/wp-load.php’);
    $args = array(
    ‘cat’ => 5, // Only source posts from a specific category
    ‘posts_per_page’ => 1 // 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(); ?>
    <span class=”post_title”><?php the_title(); ?></span>
    ” title=”<?php the_title(); ?>”>
    <?php if ( has_post_thumbnail() ) { ?>
    <span class=”post_thumbnail”><?php the_post_thumbnail(); ?></span>
    <?php } ?>
    
    <span class=”post_excerpt”><?php the_excerpt(); ?></span>
    <? }
    }
    wp_reset_postdata();
    ?>

    [Moderator note: per forum guidelines please enclose code in backticks]

    All works as I want it. However we now want a second box under the first on the same pages with posts from the other blog (video). As wp-load.php has already been included for the first blog box it gets posts from the wrong blog.

    How can I swap the database connection to the settings for the other (video) blog?

    Thanks

    • This topic was modified 9 years, 5 months ago by bdbrown.
Viewing 1 replies (of 1 total)
  • I don’t have exactly the answer you want – this isn’t something I’ve seen tried before – but this seems like a good use-case for the WordPress REST API, now part of Core since 4.7. Documentation is here: http://v2.wp-api.org

Viewing 1 replies (of 1 total)

The topic ‘Switch WordPress database outside WordPress’ is closed to new replies.