• (Actual WP version is 2.1.2 but that’s not an option above)

    I want to use wordpress as the backbone of a small site and include a site feature to search a list of electronic journal titles from a custom table within the DB through post or query string methods. I’m sorry if I’m missing something but I’m not finding a recommended way to do this inside of wordpress and am hoping someone can shed some light or point me in the right direction.

    This doesn’t seem to be a plugin since I’m not modifying anything coming in or out of wordpress itself. This would probably be considered a module in CMS terms because I’m mostly using the search return within the wordpress theme and site as a whole. If possible I’d prefer to also use the $wpdb database code instead of bringing over my previous database layer.

    Thanks in advance for any help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Assuming you understand SQL and databases, then the documentation will be handy for you:
    http://codex.wordpress.org/Function_Reference/wpdb_Class

    Thread Starter streamweaver

    (@streamweaver)

    Thanks. That’s all pretty straight forward and I want to use that for my DB calls. The DB layer I used previously uses very similar functions so I can port my stuff over pretty easily.

    My problem is where do I call these functions? I used to have a page called search.php that my form submitted to or querystring went to but I’m confused as to how I would replicate this in wordpress.

    I tried making a new page called ejsearch.php under the root directory and including the short code from the wp index.php file but if I don’t pass it a function wp recognizes it loads my home.php from the template.

    Again this doesn’t seem to be a candidate for a plugin.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You can include almost all of the wordpress code by simply adding this to the top of any PHP file:
    require('./wp-blog-header.php');

    This loads all of wordpress’s code and makes it ready. It won’t display the actual wordpress page if this is all you do.

    In order to access $wpdb from inside a function, you have to define it in the global scope, of course.

    function my_function() {
    global $wpdb;
    $wpdb->query('do stuff');
    }

    Outside of functions, you are already in the global scope and will have access to the $wpdb var immediately.

    Thread Starter streamweaver

    (@streamweaver)

    I’m sorry if I’m confusing things or not descirbing them right and appreciate the help. That works fine, I generally understand the php coding by my problem comes in tryign to tie everything together within the site for for instance.

    if file ejsearch.php under the blog root contains:`require(‘./wp-blog-header.php’);

    function testme () {
    global $wpdb;
    $result = $wpdb->get_var(“SELECT count(*) FROM wp_posts”);
    echo “<h1>”.$result.”</h1>”;
    }

    testme();`

    I get my number of posts back just fine, it’s connecting to the DB and the code seems to work well.

    My problem is that I want to include this return within the template I’m using in wordpress. That seems to happen by including the line:define('WP_USE_THEMES', true); above the ‘require’ line in the above code.

    When I do this though it loads my theme home.php instead and gives me no return.

    Thanks again for the help and sorry if I’m being thick headed.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    My problem is that I want to include this return within the template I’m using in wordpress.

    Oh.

    Put your functions into the theme’s functions.php file. If there is no functions.php file in your theme, then create one like so:

    <?php
    function testme () {
    global $wpdb;
    $result = $wpdb->get_var("SELECT count(*) FROM wp_posts");
    echo "<h1>".$result."</h1>";
    }
    ?>

    It is important that functions.php have no extra blank lines at the top or the bottom of the file, so double check that.

    Then, just put the call to your function into your theme wherever you want it to show up.

    The theme, being already part of WordPress, gets that functionality automatically. No need to include anything.

    Thread Starter streamweaver

    (@streamweaver)

    Sorry for the ping-pong here. I understand that gives me a function but I’m still at a loss as to how to call it.

    What I want is a file called ejsearch.php that I can pass some variables to that have nothing really do do with wordpress and it will query some tables in the database that are not a part of wordpress. I want to take the results of that query and display them on that page.

    I’d like to do this using the wordpress DB class and displayed using my wordpress theme.

    In the example you just gave I would need to be calling some wordpress query like a page number or something (if I understand it right anyway). I suppose I could just make a blank page in wp and don’t include any of the loop returns, instead returning my own functions and assign that page the template with those functions. That’s pretty hacky though and if I were to every transfer out I don’t want to be dependent on a wordpress URL (i.e. having to include the pagecall to a non-existant page).

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    First it’s:
    My problem is that I want to include this return within the template I’m using in wordpress.

    Then it’s:
    What I want is a file called ejsearch.php that I can pass some variables to that have nothing really do do with wordpress and it will query some tables in the database that are not a part of wordpress.

    You keep asking for contrary things. Do you want to put it in WordPress or not? If it’s part of a WordPress page then it must be part of WordPress.

    Quite simply: You can either be part of WordPress and get the template, or you can not be part of wordpress and not get the template (though you can still get the functions). But you cannot do both simultaneously.

    I just don’t understand what it is that you want to do here. You can query any table you want, so that’s not the issue. And the function calls you make can do anything they want, so that’s not the issue either.

    What is it that you want to do which you cannot do as part a normal WordPress theme?

    In the example you just gave I would need to be calling some wordpress query like a page number or something (if I understand it right anyway).

    If you’re going to display the WordPress theme, then *yes*, it has to be some sort of WordPress request. Whether it be a Page, or a single Post, or even a category archive, it doesn’t matter. For it to display with the WordPress theme, it must actually be part of the WordPress theme.

    Read up on creating a custom page template. You create a php file in your theme, put your custom search-handling code in it, and then create a Page in WP and assign it that template. Then that Page will be accessible via nice-URIs, you can pass it additional parameters (if WP doesn’t strip them out, that is…), and if you use the header and footer properly it becomes a normal themed page.

    Note that in combination with a plugin you can extend this to be extremely powerful (plugins are a generic term, they can modify core functionality, extend it, or become completely separate functionality like a forum or shopping-cart…).

    -d

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Using wordpress for custom database queries?’ is closed to new replies.