Create SQL query for Wordpress Multisite

Viewed 152

I'm trying to create a SQL query for a Wordpress multisite. I just don't get an answer.

The multisite has an id of 2 and the table is also named 2. Can someone help my with it?

If I change wp_2_posts, wp_2_postmeta to wp_posts, wp_postmeta, then it will work but only for the multisite with id = 1. I try a lot but not correct result.

<table class="tg">
    <tr>
        <th colspan="4" class="tg-dqty"> <b>Vandaag: <?=$date?> <!–– automatic date ––> </b>
        </th>
    </tr>
    <tr>
        <td class="tg-apa9">Datum/Tijd</td>
        <td class="tg-apa9">De klant</td>
        <td class="tg-apa9">Kapper</td>
        <td class="tg-apa9">Betaling</td>
    </tr>

<?php
$query_res = "SELECT * FROM wp_2_posts as wp,wp_2_postmeta as wpm,wp_blogs as blg WHERE wp.ID=wpm.post_id and wpm.meta_key='_sln_booking_date' and wpm.meta_value='$date' and blg.blog_id='2'";

$results_d = $wpdb->get_results($query_res);

foreach($results_d as $resultsd) 
{
    $post_id = $resultsd->ID;

    $first_name = get_post_meta($post_id, '_sln_booking_firstname', true);
    $last_name = get_post_meta($post_id, '_sln_booking_lastname', true);
    $date = get_post_meta($post_id, '_sln_booking_date', true);
    $time = get_post_meta($post_id, '_sln_booking_time', true);

    $services = get_post_meta($post_id, '_sln_booking_services', true);
    $attendants = array();

    foreach($services as $service)
    {
        $attendants[] = $service['attendant'];
    }

    $attendants = array_unique($attendants);

    $attendant_names = '';

    if(count($attendants) > 0)
    {
        $attendants = implode(',', $attendants);

        $query_res = "SELECT post_title FROM `wp_2_posts`, `wp_blogs`
                      WHERE post_type='sln_attendant' AND ID IN ($attendants) AND blg.blog_id = '2'";

        $results_a = $wpdb->get_results($query_res);

        $disattendants = array();

        foreach($results_a as $result)
        {
            $disattendants[] = $result->post_title;
        }

        $attendant_names = implode(',', $disattendants);

        //print_r($results);
    }

    $status = $resultsd->post_status;
    $statuses = array('sln-b-paid'=>'Betaald','sln-b-paylater'=>'Betaal later','sln-b-canceled'=>'Geannuleerd','sln-b-confirmed'=>'Bevestigd');

    $classes = array('sln-b-paid'=>'tg-jsol','sln-b-paylater'=>'tg-8a9r','sln-b-canceled'=>'tg-jsolc','sln-b-confirmed'=>'tg-jsol');
?>
<tr>
    <td class="<?=$classes[$status]?>">Om <?=$time?> uur<br><?=$date?></td>
    <td class="<?=$classes[$status]?>"><?=$first_name?> <?=$last_name?></td>
    <td class="<?=$classes[$status]?>"><?=$attendant_names?></td>
    <td class="<?=$classes[$status]?>"><?=$statuses[$status]?></td>
</tr>

<?php
}    
?> 
</table>
0 Answers
Related