I have a got custom page template which displays a drop down box that has list of countries.A user selects the box and clicks sumbit.Page then then displays the relevant details for that country .IT has also displays discription which is stored in a the database.

The following sql queries are used to display data.

            $id = mysql_escape_string($_GET['id']);
        $row_RsInt = $wpdb->get_results("SELECT * FROM wp_num_countries",ARRAY_A);
        $totalRows_RsInt = count($row_RsInt);

    <form id="formITFS" name="formITFS" method="post" action="">
                                <select class="itfsDrp" id="country" name="country">
                                    foreach($row_RsInt as $row_RsInts){
                                        $selected = $_POST['country'] == $row_RsInts['countryID'] ? 'selected="selected"' : '';
                                        echo '<option value="'.$row_RsInts['countryID'].'" '.$selected.'>'.$row_RsInts['country'].'</option>';
                                </select><input name="imageField2" id="imageField2" src="http://xxxxxxxx/wp-content/themes/responsive/images/rates.png" type="image">

        $country_id = $_POST['country'];
        $country_id = 1;

    $countryinfo = $wpdb->get_row("SELECT * FROM wp_num_countries WHERE countryID = ".$country_id);
    <form action="" method="POST" name="form" id="formn">
    <div id="ITFSworks">

<?php echo $countryinfo->desc; ?>

I want to create a seperate page for each country .The page will contain more text.When a user goes to this new page he gets the same layout as the custom page template.

So if my perma link is France-page , i want the drop down to be pre-selected with france and info of france dispayed under and so forth..

How can i best achieve this. I am using the latest wordpress.

Whats the best way to do this.

2 Answers 2


I can't comment yet, so I'll tell you here what you can change immediately:

$countryinfo = $wpdb->get_row("SELECT * FROM wp_num_countries WHERE countryID = ".$country_id);


$countryinfo = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_num_countries WHERE countryID = %d", $country_id));

because you directly use $_POST variable, so $wpdb->prepare will format and prepare your query string. But that code looks fine for me (you should only use indents as they are expected, because of easier reading :) )

If I were you, I would make a page and put this code in it. For example, page-countries.php, and page with title Countries, slug 'countries'. In that form put submit button and that's it. Also, I would test for if(isset($_POST['country'])) instead of if($_POST['country']).


If I understand you, if you were to create a custom post type for your countries instead of trying to hack this together with your own tables then much of this would be solved automatically. WordPress will handle the rewriting for you. Given the information provided I don't think I can get anywhere close to working code but if you follow the examples in the Codex you should get a pretty good start. Here is one of them:

function codex_custom_init() {
  $labels = array(
    'name' => 'Books',
    'singular_name' => 'Book',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Book',
    'edit_item' => 'Edit Book',
    'new_item' => 'New Book',
    'all_items' => 'All Books',
    'view_item' => 'View Book',
    'search_items' => 'Search Books',
    'not_found' =>  'No books found',
    'not_found_in_trash' => 'No books found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Books'

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'book' ),
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )

  register_post_type( 'book', $args );
add_action( 'init', 'codex_custom_init' );

Replace the references to 'book' with 'countries' and you are on your way. Supplement the ordinary post data with Custom Meta Fields, possibly registering your own metaboxes for convenience and you should be a long way towards getting this done.

If you don't want to do that then you can create a "Page" and custom page template to handle this data and pass your country data via a $_GET string.

You could also use WP_Rewrite to generate pretty permalinks, but that is complex. The best solution, in my opinion, is the Custom Post Type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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