I want to view the details of a product in single.php. The product is inserted in a newly added table "products" in the wordpress database and it has no post_type or post_id.

Dani AI

Generated

Short summary: single.php renders WordPress posts (the Loop). Because your products live in a standalone products table with no post_type/post_id, WordPress won’t load them automatically. There are two practical patterns: integrate products into WP (recommended for permalinks, admin UI, meta, SEO) or keep the custom table and fetch rows yourself from a template — but do that securely and map rows to WP posts when possible. ’s suggestion to avoid a manual table is the right long-term advice; below are concrete, safe ways to implement either path and how to store price/date range.

If you convert to a custom post type or create a WP post per product, store price and date range in post meta (or with ACF). Retrieving them on single.php is simple and safe:

$price = get_post_meta( get_the_ID(), 'price', true );
$start = get_post_meta( get_the_ID(), 'date_start', true );
$end   = get_post_meta( get_the_ID(), 'date_end', true );

echo esc_html( $price );
echo esc_html( date_i18n( 'F j, Y', strtotime( $start ) ) ) . ' – ' . esc_html( date_i18n( 'F j, Y', strtotime( $end ) ) );

If you must keep the separate table, query it with $wpdb using prepared statements and a reliable mapping from the current request (for example, a product_id stored in postmeta or passed via a sanitized query var). Example pattern to fetch a row safely:

global $wpdb;
$table = $wpdb->prefix . 'products';
$product_id = isset($_GET['product_id']) ? intval($_GET['product_id']) : 0;

$product = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $product_id ), ARRAY_A );
if ( $product ) {
    echo esc_html( $product['title'] );
    echo esc_html( number_format_i18n( (float)$product['price'], 2 ) );
    echo esc_html( date_i18n( 'F j, Y', strtotime( $product['start_date'] ) ) ) . ' – ' . esc_html( date_i18n( 'F j, Y', strtotime( $product['end_date'] ) ) );
}

Notes and troubleshooting: always use $wpdb->prepare and escape output with esc_*. Prefer mapping each product row to a WP post (store the table row id in postmeta) so single.php can stay compatible and you can add metaboxes/datepickers in the admin (use ACF or add_meta_box). If queries return empty, check $wpdb->last_error and $wpdb->last_query, ensure you used the correct table prefix, and enable WP_DEBUG while developing.

Recommended Answers

All 3 Replies

For a wordpress site, please do not add the table manually, normally what we done is use a custom post type by nested the code into functions.php

//add "product" custom post
    add_action( 'init', 'create_product_post_types' );
    function create_product_post_types() {
       register_post_type( 'product',
           array(
               'labels' => array(
               'name' => __( 'product' ),
               'singular_name' => __( 'product' ),
               'add_new' => __( 'Add New' ),
               'add_new_item' => __( 'Add new product' ),
               'edit' => __( 'Edit' ),
               'edit_item' => __( 'Edit product' ),
               'new_item' => __( 'New product' ),
               'view' => __( 'View product' ),
               'view_item' => __( 'View product' ),
               'search_items' => __( 'Search product' ),
               'not_found' => __( 'No product found' ),
               'not_found_in_trash' => __( 'No product found in Trash' )
               ),
               'public' => true,
               'show_ui' => true,
               'publicly_queryable' => true,
               'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
               'rewrite' => array("slug" => "product"),
               'taxonomies' => array('post_tag')
           )
       );
    }

This will create a post with the name 'product'. Then using the query_posts and the_permalink to get the link for single page and display the_content()

@ lps, thanks for your reply and it really show me some light in the darkness.
But how can we add price and date range for the product like this?
any help... thanks in advance.

try to use the custom fields that will only be available after you select it from the 'screen option' at your right top corner. But if you wish for a better user effect like a date picking tools etc, you should research on 'metabox' where you will be able to create your own layout for the extra fields.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.