0

Sorry for my english, I've created a table mysql with data. Name of my table is 'test'. Field is: id, name, description

I would like to show them in a post page with a plugin.

For example: if i call a url

mysite.com/test/data1

i would like:

  • the_ID() to be test.id

  • the_title() to be test.name

  • the_content() to be test.description

it is possible?


UPDATE:

I have this code:

function prefix_movie_rewrite_rule() {
    add_rewrite_rule( 'test/([^/]+)', 'index.php?test=$matches[1]', 'top' );
}

add_action( 'init', 'prefix_movie_rewrite_rule' );


function prefix_register_query_var( $vars ) {
    $vars[] = 'test';

    return $vars;
}

add_filter( 'query_vars', 'prefix_register_query_var' );

Now:

how do I open the wordpress post page with data of my table?

4
  • You don't need to create a table for that, you can use a custom post type : codex.wordpress.org/Post_Types#Custom_Post_Types
    – mmm
    Commented Nov 4, 2017 at 16:20
  • I already have thousands of data in the table...
    – gasmor
    Commented Nov 4, 2017 at 17:37
  • The ..../data1 part of the url is a primary key or some unique value on your custom table? I'm not sure what you want to do. Commented Nov 4, 2017 at 20:57
  • Yes: the ..../data1 part of the url is a primary key.
    – gasmor
    Commented Nov 5, 2017 at 5:42

1 Answer 1

0

You can perform cusom SQL queries on the Wordpress database using WPDB.

For example:

$row = $wpdb->get_row("SELECT * FROM $wpdb->test WHERE id = 123");

You can then access your id, name and description fields like so:

echo $row['id'];
echo $row['name'];
echo $row['description'];

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.