transfield Posted May 23, 2015 Share Posted May 23, 2015 Hi,I have a Wordpress plugin which I want to edit. My code is below. I have made comments in the areas that I need help in coding. I would appreciate your assistance in showing me the correct way to write the code. Thanks <?php global $wpdb; //this should be correct //I need help to convert the 2 lines below to a Wordpress friendly query $query1="SELECT * FROM wp_users WHERE user_login LIKE '%z%'"; $result1 = mysql_query($query1); //I want to put the search results into the example below if ( $user_login == 'admin' || $user_login == 'joe' ) { $do_something; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/ Share on other sites More sharing options...
QuickOldCar Posted May 24, 2015 Share Posted May 24, 2015 Surely that is not your entire plugin. Did you check out the wordpress codex about plugins? If you really wanted to create your own plugin for this the wp queries get complex and a few joins on the tables. Here is just an example of a plugin that will show only if is admin or network superadmin <?php /* Plugin Name: I Am Admin Plugin URI: http://site.com Description: Shows a message are admin Author: admin@site.com Version: 1.0 Author URI: http://site.com */ function i_am_admin(){ if(current_user_can('manage_options') || is_super_admin()) { echo " <style type='text/css'> #admin_panel { top: auto !important; bottom: 0; } p.admin_panel { top: -20; width: 100%; background-color:white; color: red; border-style:solid; border-width:1px; text-decoration:none; text-shadow:1px 1px 0px #000000; text-align:center; margin-top:40px; margin-bottom:0px; margin-right: auto; margin-left: auto; padding-top: 20px; padding:0px; } </STYLE>"; echo "<div><p class='admin_panel'>I am The Admin</p></div>"; } } add_action('wp_head', 'i_am_admin'); ?> As for the search queries you can download a search plugin and examine what they used.(or use theirs and wrap it with the check for admin) https://wordpress.org/plugins/wp-user-listing-and-searching/ Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512535 Share on other sites More sharing options...
transfield Posted May 24, 2015 Author Share Posted May 24, 2015 I've figured out the 1st part which is: <?php global $wpdb; $my_table = $wpdb->prefix."users"; $my_query = "Select user_login from $my_table where user_login LIKE '%z%' "; $my_results = $wpdb->get_results($my_query); ?> Now, in second part, how do I put the search results into the foreach loop? Lets assume that I have 100 records in the search results. How do I loop through all the 100 records using the code below: <?php foreach($my_results as $user_data) { if($user_data -> user_login === 'admin' || $user_data -> user_login === 'joe' //all hundred records need to go inside here. How do I do that?) // do something } ?> Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512549 Share on other sites More sharing options...
QuickOldCar Posted May 24, 2015 Share Posted May 24, 2015 (edited) Are you looking to echo the name? You say want 100 results but the if statement would only show for admin or joe. foreach($my_results as $user_data){ if($user_data -> user_login === 'admin' || $user_data -> user_login === 'joe'){ echo "Admin<br />"; }else{ echo $user_data->name."<br />"; } } Edited May 24, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512562 Share on other sites More sharing options...
transfield Posted May 25, 2015 Author Share Posted May 25, 2015 (edited) I want all the search results to go into this line: if($user_data -> user_login === 'admin' || $user_data -> user_login === 'joe'){ Edited May 25, 2015 by transfield Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512586 Share on other sites More sharing options...
QuickOldCar Posted May 25, 2015 Share Posted May 25, 2015 Now you lost me as to what you want. Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512591 Share on other sites More sharing options...
transfield Posted May 25, 2015 Author Share Posted May 25, 2015 (edited) Never mind. You never answered any of my questions anyway. You were more interested in deviating from the original issue for reasons best known to you. Edited May 25, 2015 by transfield Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512600 Share on other sites More sharing options...
QuickOldCar Posted May 25, 2015 Share Posted May 25, 2015 Not here to argue but to help, I'm moving on and good luck. Quote Link to comment https://forums.phpfreaks.com/topic/296462-writing-a-query-for-wordpress/#findComment-1512601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.