简体   繁体   中英

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

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);

我知道了

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM