0

I'm trying to create a custom template. Iv'e searched a lot but cannot find how to query the database once the get_header() is added to the page.

What I am looking for is for PROPER ways to query the database.

Thanks

Update:

basically i added my own tables to the database. Including the header in my template SHOULD automatically include also the necessary database connection used by wordpress. My question i how to use this open database connection to query the database and read data from my custom tables. for instance, how is the mysql-connection object called, and how do i execute a query the proper way. (I know how to make a query in general, but i want to avoid to use unneded code if it is already provided, and i would like to avoid spaghetti-coding.)

this is what i have so far:

global $wpdb;
$querystr = "SELECT *FROM mytable WHERE name ='".$name."'";
echo $querystr;
$gmdata = $wpdb->get_results($querystr, OBJECT);

foreach ($gmdata as $gm)
{
echo $gm[1]."<br>";

}

Thanks!

  • 1
    Querying database is in no way connected with or affected by header. Please elaborate on what you are trying to do and which issues or unexpected results you are facing. – Rarst Feb 11 '14 at 20:59
  • including the header should also include all the database connection part, where i can use wordpress's open mysql connection to execute queries.. updated the question to clearify – sharkyenergy Feb 12 '14 at 16:56
  • From what i've read here (codex.wordpress.org/… ) you are doing it right: you use the $wpdb object. Since you are using custom tables, there isn't much you can reuse from the wordpress querying methods. – pixeline Feb 12 '14 at 20:42
1

Your question is vague: i'll try to be as specific as possible.

The basic rule is: if you need to modify the default query, use pre_get_post If you need to display another list of results than the one linked to your template url, use get_posts.

I find this documentation to be quite useful http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

Doc on pre_get_posts http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Doc on get_posts http://codex.wordpress.org/Template_Tags/get_posts

  • thank you! i updated the question to clarify. sorry if it wasnt too clear from my basic question. – sharkyenergy Feb 12 '14 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.