Custom Query to Return usermeta information
-
Please redirect me if this is not appropriate for this forum. And FYI, more than a decade ago, I used PL-SQL quite a lot using the application PL-SQL Developer. My experience with php is quite limited.
What I wish to do is have a custom page (for example, “dbtest.php”) and pull meta values from the WP database. I’m only being partially successful. All the data I need is in the usermeta table. At this time, I don’t need to join any other tables.
I need to return the meta values for the following meta keys:
- first_name
- last_name
- nickname
- textarea_zknsk
- textarea_o0nn8
- textarea_fjy8e
I only want to return the records where one, two, or three of the three textarea fields have values.
Following is the code I’m using that partially works. I’m having two major issues.
- I need to use a loop. At the start, you will see the query that returns the max user_id in the table. But when I substitute in “IDmax” for the “$x” in the loop, it does not work. How do I increment the loop and then stop at at the IDmax?
- The loop pulls records for every user_id. I need a CASE (IF) set that will constrain the results to only user_ids where one or more of the textarea fields are populated. If the three textarea field’s meta values are empty then I don’t want the record. But I’m at a loss as to how to construct it.
// Return Metadata ONLY. $IDmax = $wpdb->get_results("select max(user_id) FROM $wpdb->usermeta"); for ($x = 0; $x <= 18; $x++) { $results1 = get_user_meta( $x, 'first_name', true ); $results2 = get_user_meta( $x, 'last_name', true ); $results3 = get_user_meta( $x, 'nickname', true ); $results4 = get_user_meta( $x, 'textarea_zknsk', true ); $results5 = get_user_meta( $x, 'textarea_o0nn8', true ); $results6 = get_user_meta( $x, 'textarea_fjy8e', true ); echo "<pre><strong>First Name: </strong>"; print_r($results1);echo "</br>"; echo "<strong>Last Name: </strong>"; print_r($results2);echo "</br>"; echo "<strong>Nickname: </strong>"; print_r($results3);echo "</br>"; echo "<strong>Board Member Notes: </strong>"; print_r($results4);echo "</br>"; echo "<strong>Board Secretary Notes: </strong>"; print_r($results5);echo "</br>"; echo "<strong>Board Treasurer Notes: </strong>"; print_r($results6);echo "</pre>"; }
Any help will be greatly appreciated.
- The topic ‘Custom Query to Return usermeta information’ is closed to new replies.