0

I would like to know how to extract data from Wordpress, unserialize and show it in a table. Below is my starting point. The columns address, group and divider all have serialize data in.

I have read about get_user_meta() but not sure how to use it.

Any suggestion welcome.

[insert_php]
    //connection details
     require_once('/home/xxx/private/xxx.php');
     
        $query = "SELECT MAX(CASE WHEN wp_usermeta.meta_key = 'first_name' then wp_usermeta.meta_value ELSE NULL END) as first_name, 
                        MAX(CASE WHEN wp_usermeta.meta_key = 'last_name' then wp_usermeta.meta_value ELSE NULL END) as last_name,
                        MAX(CASE WHEN wp_usermeta.meta_key = 'pm_field_13' then wp_usermeta.meta_value ELSE NULL END) as address,
                        MAX(CASE WHEN wp_usermeta.meta_key = 'pm_field_101' then wp_usermeta.meta_value ELSE NULL END) as group,
                        MAX(CASE WHEN wp_usermeta.meta_key = 'pm_field_105' then wp_usermeta.meta_value ELSE NULL END) as divider,
                        wp_users.user_login            
                        FROM wp_users
                        LEFT JOIN wp_usermeta
                        ON wp_users.ID = wp_usermeta.user_id
                        GROUP BY wp_users.user_login"; 
       $filter_Result = mysqli_query($connect, $query) or die("Error: ".mysqli_error($connect));
       return $filter_Result;
       $num_rows = mysqli_num_rows($search_result);
    
               <table id="indextable" style="table-layout: fixed; width: 100%;" >
                    <thead>
                    <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Address</th>
                        <th>Group</th>
                        <th>Divider</th> 
                     </thead>
                    </tr>
    
          <!-- populate table from mysql database -->
                    <?php while($row = mysqli_fetch_array($search_result)):?>
                    <tr>
                        <td><?php echo $row['first_name'];?> </td>
                        <td><?php echo $row['last_name'];?></td>
                        <td><?php echo unserialize($row['address']); ?></td>
                        <td><?php echo unserialize($row['group']);?></td>
                        <td><?php echo unserialize($row['divider']);?></td>             
                    </tr>
    
                    <?php endwhile;?>
                </table>
    
    [/insert_php]
4
  • You should not use plugins that let you write PHP inside post content using shortcodes, they're incredibly dangerous and a major security hole. They're also a development dead end. For example, the correct way to query for this data would be via WP_User_Query or get_users, but that's not possible with [insert_php]. If you need to embed code in a post, build an actual shortcode, the conversion process is straightforward and will make things much more secure and easier. Otherwise you can't use WordPress functions – Tom J Nowell Sep 18 '20 at 13:22
  • Thank you Tom. I will do research on WP_User_Query and get-users.I am new at this. – Ryks Sep 18 '20 at 14:13
  • You cannot use those functions with the PHP shortcodes. You need to migrate your code to a shortcode first. Look into add_shortcode, which would go in your themes functions.php. If you do not do this, then you will get PHP errors for missing functions and classes – Tom J Nowell Sep 18 '20 at 15:28
  • Thanks, Tom. Will investigate further – Ryks Sep 19 '20 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.