Mina Ragaie
Mina Ragaie

Reputation: 475

How to write a select statement in Wordpress file like "function.php"

I tried this code in phpmyadmin before and it works fine

SELECT meta_value
FROM wp_postmeta
INNER JOIN wp_posts ON post_id =564
WHERE meta_key =  'max_person';

But I don't know how to insert it in function.php

I insert this code and it works

<? PHP 
    $postid = get_the_ID();
    $maxperson = $wpdb->get_row( $wpdb->prepare(
    "SELECT meta_value FROM wp_postmeta inner join wp_posts ON post_id= %d WHERE meta_key = 'max_person'", $postid
        ) );
?> 

But when I var_dumped $maxperson var_dump ($maxperson); it returns

object(stdClass)#634 (1) { ["meta_value"]=> string(1) "5" } the result should be "5" i dont understand what that means

Upvotes: 1

Views: 182

Answers (2)

Mina Ragaie
Mina Ragaie

Reputation: 475

i got it

$value = (integer) $maxperson->meta_value;

Upvotes: 1

Dhruv Kapatel
Dhruv Kapatel

Reputation: 893

You have to use custom query to get post by post ID or any other attributes. go through WP documentation for detail. http://codex.wordpress.org/Class_Reference/WP_Query

$args = array(
'p' => 42, // id of post
'meta_key' => 'max_person');
$my_posts = new WP_Query($args);

Upvotes: 2

Related Questions