• I am writing a custom database page outside wordpress, but would like to import my wordpress theme and also pull in a post for this page.

    First, I include this to pull in the wordpress theme and other functions:
    require($_SERVER[‘DOCUMENT_ROOT’] . ‘/wp-blog-header.php’);

    now, if I include a call a wordpress function here, something like get_post($my_id); it works great.

    But, the same function doesn’t return results if I insert it after this mysql call:
    mysql_select_db($dbname);

    Which is a problem since I need it to be included after that call.

    Any thoughts on why? Is there anything I can do to get a post after I connect to the other database? Closing the mysql connection doesn’t change anything, unfortunately.

    Thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Yes. I think you’ve selected a different database, haven’t you? So a query on the WP database to get posts won’t work.

    If I’m right, I guess the easiest solution would be to put your custom tables in the WP database.

    Or use multiple connections, somehow although I don’t see immediately how you could tell get_post() which connection to use. It’s presumably using the last one opened. So I suppose you could surround the call to get_post() …

    Running out of steam here.

    Cheers

    PAE

    Thread Starter jbrehm2

    (@jbrehm2)

    I figured I was selecting a different database which was causing the trouble. Somehow using multiple connections is, as far as I can tell, the only solution. So, I guess my question is if there’s some handy wordpress function that I’m not finding to do this … but guessing there’s not.

    Writing this page without using built in wordpress functions will work, just a lot more difficult.

    Thanks for your reply.

    Have you tried creating a new connection using standard PHP in your own code. If get_post() uses the last connection opened, that should do it.

    Then for your own code, you just make sure you use the other connection.

    I’m struggling to find a reason why you must make your call to get_post() after your mysql_select_db() call with no opportunity to reset things. Why can’t you do:

    $selected_db = mysql_select_db('my_db', $my_conn);
    // do your stuff with your own database
    
    $selected_db = mysql_select_db('wp_db', $wp_conn);
    $post = get_post();
    // other stuff
    
    // reset the selected db if necessary
    $selected_db = mysql_selecct_db('my_db', $my_conn);

    The code’s off the top of my head, so it may not be entirely correct; but I think you can see what I’m getting at.

    Interesting one, though.

    Cheers

    PAE

    Thread Starter jbrehm2

    (@jbrehm2)

    That looks like it would work … except I actually need to query the wordpress db while I loop through the old db. I need to compare a variable from each record with the post ID. This is sort of an old system written for something else where I’m trying work in a little wordpress integration. The ability to call wordpress functions while I am looping through the data would make the transition super-easy. The other options require a little work, but can be done.

    I guess I could also get the data from the old DB, put it in a variable, close the old connection and open the WP connection. Not as easy of a switch since that’s not how the code was originally written, it will work if it needs to — but I’d like to take the easy rout if possible.

    That would have been my suggestion. Get everything into an array or object and iterate through that.

    Best of luck with it. I feel your pain. I have some integration projects of my own to do and I’m not looking forward to them at all. I’m hoping to use CodeIgniter MVC for the data-driven parts; but there’s a lot of unanswered questions…

    Cheers

    PAE

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Themes & functions outside wordpress & mysql_connect’ is closed to new replies.