在 Wordpress 查询中显示具有特定参数的数据

Showing data with specific parameter in Wordpress Query

在那里我有profile_driver,我想让这个数据在我第一次输入参数时显示。例如输入 'id'

$command = $_GET['command'];
switch ($command) {
    case 'profile_driver':
    if(!empty($_REQUEST['id'])) {
           $data = array( "api_status" => 0, "api_message" => "cant Find data");
           echo  json_encode($data);
        } else {
           $loop = new WP_Query( 
                array(
                        'post_type'    => 'drivers'
                    )
                ); 
            if( $loop->have_posts() ) :

                $data = array( "api_status" => 1, "api_message" => "success");
                $meta = array();
                while ( $loop->have_posts() ) : $loop->the_post();

                    $meta[] = array(
                        "id"         => get_the_ID(),
                        "post_name"  => get_the_title(),
                        "username"      => get_post_meta( get_the_ID(), 'username', true ),
                        "password"      => get_post_meta( get_the_ID(), 'password', true ),
                        "email"      => get_post_meta( get_the_ID(), 'email', true ),
                        "phone"      => get_post_meta( get_the_ID(), 'phone', true ),
                    );

                endwhile;
            endif;

            echo  json_encode($meta);
        }
break;
}

当我尝试在邮递员中 运行 时(我有正确 ID 的数据):

有人帮我改进我的代码,这样我的代码就可以像我想要的那样工作吗?

在将数据返回到网络服务之前,您需要在此行 case 'profile_driver': 之后放置 if 条件。

尝试转储 var_dump($_REQUEST) 以检查 id 参数是否在请求中 OR 它为空。

if(!empty($_REQUEST['id'])) {
   echo 'Please enter id';
} else {
   // your rest of the code goes here
}

你还需要改变循环。

$data = array("api_status" => 1, "api_message" => "success", "result" => "");
if( $loop->have_posts() ) :
    while($loop->have_posts()) : $loop->the_post();
        $data['result'][] = array(
            "id"        => get_the_ID(),
            "post_name" => get_the_title(),
            "username"  => get_post_meta(get_the_ID(), 'username', true),
            "password"  => get_post_meta(get_the_ID(), 'password', true),
            "email"     => get_post_meta(get_the_ID(), 'email', true),
            "phone"     => get_post_meta(get_the_ID(), 'phone', true),
        );
    endwhile;
endif;
echo  json_encode($data);