WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Two blogs – One database – How to extract data?

[Resolved] Two blogs – One database – How to extract data?

  • Here is my situation. I’ve installed two seperate WP v2.0.1 blogs to a single mysql database. One is defaulted to “wp_” the other is “rv_”.

    Within both install directories I have “my-hacks.php” which contains a function that gives an excerpt from a post. From the main page of my website I call each of the functions (that are named differently) to show the latest post.

    Problem is the only one that is working is the function from the original install (wp_ prefixed tables). My question is do I have to alter the code to make it work with the “rv_” prefixed tables?

    Here is the code (note both are the same only the functions are named differently). Oh yeah, I also know the code (which isn’t mine) isn’t exactly optimized . . .

    function get_recent_posts($no_posts = 1, $before = '<li>', $after = '</li>', $show_pass_post = false, $skip_posts = 0) {
    global $wpdb, $tableposts, $tablepost2cat;
    $request = "SELECT ID, post_title, post_content, category_id FROM $tableposts, $tablepost2cat WHERE post_status = 'publish' AND (post_id = ID AND category_id != '5')";
    if(!$show_pass_post) { $request .= "AND post_password ='' "; }
    $request .= "ORDER BY post_date DESC LIMIT $skip_posts, $no_posts";
    $posts = $wpdb->get_results($request);
    $output = '';
    foreach ($posts as $post) {
    $string = ($post->post_content);
    $cut = implode(" ", array_slice(preg_split("/s+/", $string), 0, 15)); //change last number to # of words you want displayed
    $post_title = stripslashes($post->post_title);
    $post_excerpt = stripslashes($post->post_excerpt);
    $permalink = get_permalink($post->ID);
    //$test = str_word_count($post->post_content)
    $output .= $before . '<font size=3><b>' . $post_title . '</b></font>' . $after;
    $output4 .= $before . '<div class="rightColumnSplit"></div>';
    $output1 .= $before . $cut . ' . . .' . $after;
    //$output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . $post_excerpt . '">' . $post_excerpt . '</a>' . $after;
    $output3 .= $before . '<div class="rightColumnSplit"></div>';
    $output2 .= $before . '<center><a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . ReadMore . '">' . Read_More . '</a></center>' . $after;

    }
    echo $output;
    echo $output4;
    echo $output1;
    //echo implode(" ", array_slice(preg_split("/s+/", $string), 0, 10)); //change last # for # of words to display
    echo $output3;
    echo $output2;
    }

    Thanks for any insight.

    -mike

Viewing 7 replies - 1 through 7 (of 7 total)
  • It appears that it has something to do with the “wp-config.php” file that is being used by both installations. Is there something that needs to be edited?

    Thanks.

    -mike

    Moderator Mark Jaquith

    @markjaquith

    The rv_ install should have rv_ set as the table prefix in wp-config.php

    Then, in your function, declare $wpdb global and use $wpdb->posts, $wpdb->post2cat, $wpdb->comments, $wpdb->categories etc, to reference the correct table. $tableposts etc are deprecated… you’ve likely based your function on code that is about 2 years old, or so.

    markjaquith,

    That doesn’t appear to work for me. I get invalid argument error now.

    Thanks.

    -mike

    The problem I believe is that both functions are declaring “$wpdb” globally. Is there a way around this?

    When one call is removed the other works fine, only together does the second one called not work . . . any help would be most appreciated!

    -mike

    Moderator Mark Jaquith

    @markjaquith

    hm, you’re probably include()ing both installs, which will cause problems. They need to run in their own space. Depending on your server setup, you could include() a URI. That is, set up a file for each blog that includes wp-config.php and then calls the function that displays the posts. And then in your main site, include() that URI. If it is on the same server, it should be pretty fast.

    I actually tried something similar to that. I got these errors on the mainpage:

    Fatal error: Cannot redeclare get_recent_posts() (previously declared in C:\inetpub\wwwroot\mike\my-hacks.php:17) in C:\inetpub\wwwroot\mike\my-hacks.php on line 45

    and

    Fatal error: Cannot redeclare get_recent_posts2() (previously declared in C:\inetpub\wwwroot\allen\my-hacks.php:17) in C:\inetpub\wwwroot\allen\my-hacks.php on line 45

    -mike

    Figured it out myself. Just wrote a completely seperate query to the database.

    Thanks, markjaquith, for trying to lend a hand.

    -mike

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Two blogs – One database – How to extract data?’ is closed to new replies.
Skip to toolbar