WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] extract database query into templates (11 posts)

  1. jimlongo
    Member
    Posted 2 years ago #

    Hi,
    I want to run a query inside my woocommerce templates on the backend.

    This will return the proper value if I do the following . . .

    <?php global $wpdb;
    	$wc = $wpdb->get_var( "
    		SELECT meta_value FROM wp_postmeta
                    WHERE post_id = 128 AND meta_key = '_mycustomfield' "
                                                 );
    			echo $wc;
    ?>

    I'm inside the woocommerce thankyou.php template at the moment and I want to filter not on post_id=128 but something along the lines of post_id=$order->post_id.

    I just can't get the syntax for this type of query. How do I specify the current order?

    http://wordpress.org/extend/plugins/woocommerce/

  2. Roy Ho
    Member
    Posted 2 years ago #

    Hmmmm why not just use

    $value = get_post_meta( $order->post_id, '_stored_value_name', true );
  3. jimlongo
    Member
    Posted 2 years ago #

    Hi, that's simpler, but it still doesn't return anything.

  4. Roy Ho
    Member
    Posted 2 years ago #

    Well you have make sure this data exists first...But that is the correct way to pull post meta out of the db.

  5. jimlongo
    Member
    Posted 2 years ago #

    I know the data exists, I can query the database and return it.

    SELECT meta_value FROM wp_postmeta
    WHERE post_id = 128 AND meta_key = '_mycustomfield'

  6. Roy Ho
    Member
    Posted 2 years ago #

    If the data exists, the function I mentioned will absolutely pull that data out.

    Based on your example query, it should look like:

    $value = get_post_meta( 128, '_mycustomfield', true );
  7. jimlongo
    Member
    Posted 2 years ago #

    Yes the example you just posted pulls the data. That is if I put the literal 128.
    But it doesn't work with $order->post_id

  8. Roy Ho
    Member
    Posted 2 years ago #

    Well then you should check into the post id if that is valid and set...

  9. Roy Ho
    Member
    Posted 2 years ago #

    Try $order->id instead...

  10. jimlongo
    Member
    Posted 2 years ago #

    Are you referring to the wp_posts table?
    There is a row with ID 128

    I am in the woocommerce thankyou.php template and it is pulling information such as <?php echo $order->get_order_number(); ?> successfully.

  11. jimlongo
    Member
    Posted 2 years ago #

    That's It!

    $order->id makes either versions of the code run,
    get_post_meta as well as $wpdb->get_var

    Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic