I have created a new table within the same database that contains the Wordpress tables. How can I query this specific table from a custom php page? The page can't seem to connect to the db. It's been a while since I have had to do any php/mysql coding, so forgive my ignorance.
<?php
global $wpdb;
$sql = "SELECT * FROM table_name";
$result = mysqli_query($wpdb, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["ID"]. " " . $row["content"]. "<br>";
}
} else {
echo "0 results";
}
?>
$wpdb is an instance of the wpdb class. You make calls to it like the following:
global $wpdb;
//Prepare a SQL Query
$query = $wpdb->prepare("SELECT * FROM my_custom_table WHERE column_foo = %s", $someVariable);
$results = $wpdb->get_results($query);
foreach($results as $result) {
//do a thing
}
Related
I want to display a number of entries for a specific uid.
this is my function but it can not display any value
function anscount()
{
global $wpdb;
$table_name = $wpdb->prefix.'pte_quiz';
$sql = "SELECT Count(*) AS count FROM $table_name WHERE uid =1";
echo "Count Number: ";
}
add_action('init','anscount');
try this way brother :
function anscount()
{
global $wpdb;
$table_name = $wpdb->prefix.'pte_quiz';
$sql = "SELECT Count(*) AS count FROM $table_name WHERE uid =1";
$myrow = $wpdb->get_results($sql);
if (isset($myrow[0]) && isset($myrow[0]->count)) {
echo "Count Number: ".$myrow[0]->count;
}
}
add_action('init','anscount');
I'm trying to make a plugin for wordpress visitor counter and limit counts of visitor. I wrote this code, but counter variable increments 4 times or 5 times every time I refresh the page. Basically plugin is for free visits after free visits user should be logged in to visit website. Please check the code.
<?php
$count=0;
function ip_get(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$new_ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$new_ip = $_SERVER['REMOTE_ADDR'];
}
global $wpdb;
$table_name=$wpdb->prefix.'wbs_user_ips';
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(empty($results)){
$wpdb->insert($table_name,
array(
'user_ip'=>$new_ip,
'user_count'=>'1',
'user_status'=>'0',
)
);
}
if(!empty($results)) // Checking if $results have some values or not
{
$count=0;
foreach($results as $row){
$ip= $row->user_ip;
$count=$row->user_count;
}
if($ip==$new_ip){
echo $count++;
$wpdb->query($wpdb->prepare("UPDATE $table_name
SET user_count=".$count."
WHERE user_ip = %s",$ip));
}
}
}
?>
I think there is an error in your foreach loop.
And please make sure you use the correct hook
<?php
$count=0;
function ip_get(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$new_ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$new_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$new_ip = $_SERVER['REMOTE_ADDR'];
}
global $wpdb;
$table_name=$wpdb->prefix.'wbs_user_ips';
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(empty($results)){
$wpdb->insert($table_name,
array(
'user_ip'=>$new_ip,
'user_count'=>'1',
'user_status'=>'0',
)
);
}
if(!empty($results)) // Checking if $results have some values or not
{
$count=0;
foreach($results as $row){
$ip= $row->user_ip;
$count=$row->user_count;
if($ip==$new_ip){
echo $count++;
$wpdb->query($wpdb->prepare("UPDATE $table_name
SET user_count=".$count."
WHERE user_ip = %s",$ip));
}
}
}
}
?>
I am trying to modify the custom wordpress search by using pre_get_posts hook, so that a specific words won't be searched in product description.
add_action( 'pre_get_posts', 'exclude_search_content' );
function exclude_search_content( $Q ) {
$search_phrase = $Q->query['s'];
if( $Q->is_search() ) {
/** Dont search for $search_phrase in excerpt and product description
* search only product title, a meta key and a taxonomy.
*/
}
}
It seems to be more complex than I expected. Any ideas?
if you check posts_search reference you will find some of Contribute already wrote similar function, so i just test it and it's working fine.
function search_by_title_only($search, $wp_query)
{
global $wpdb;
if (empty($search)) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = !empty($q['exact']) ? '' : '%';
$search =
$searchand = '';
foreach ((array) $q['search_terms'] as $term) {
$term = esc_sql($wpdb->esc_like($term));
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if (!empty($search)) {
$search = " AND ({$search}) ";
if (!is_user_logged_in()) {
$search .= " AND ($wpdb->posts.post_password = '') ";
}
}
return $search;
}
add_filter('posts_search', 'search_by_title_only', 20, 2);
Hi I am using learndash Wordpress plugin. I want to get the data related to a user tht how many courses he is enrolled in and how many has he completed. Is there a way to check this? does learndash provide any solution for this or should I query data myself?
Any help is appreciated. Thanks in advance.
Please ask for any more details if you want.
You can use the following to get all course ID's the current user is currently enrolled to:
learndash_user_get_enrolled_courses(get_current_user_id())
I found that all is stored inside the table learndash_user_activity and you can query that table to get user stats.
For example I get the list of the in-progress users for a given course with the following query:
public static function get_inprogress_users_for_course( $course_id )
{
global $wpdb;
if( empty( $course_id ) ) return [];
$results = $wpdb->get_results( "SELECT `user_id` FROM `" . $wpdb->prefix . "learndash_user_activity` "
."WHERE `course_id` = '" . intval( $course_id ) . "' "
."AND `activity_type` = 'lesson' "
."GROUP BY `user_id`" );
return $results;
}
In the same way, you can get all the IDs of the users ever enrolled to a course changing activity_type from 'lesson' to 'course' in the query, and if you want to only get enrolled course by a user, you can add the user_id to the query, like this:
public static function get_courses_for_user( $user_id )
{
global $wpdb;
if( empty( $course_id ) ) return [];
$results = $wpdb->get_results( "SELECT * FROM `" . $wpdb->prefix . "learndash_user_activity` "
."WHERE `user_id` = '" . intval( $user_id ) . "' "
."AND `activity_type` = 'course' "
."GROUP BY `course_id`" );
return $results;
}
I know this is not exactly what you were searching for, but it could still be useful.
You can return anything using wp_query. Try this:
function wpso49370180_get_course_name($courseid) {
global $wpdb;
$user_id = the_author_meta( 'ID' ); //alt method below
$query_course = "SELECT post_title
FROM wp_posts
WHERE post_type = 'sfwd-courses'
AND post_status NOT IN ( 'trash','auto-draft','inherit' )
AND post_author='$user_id' LIMIT 10";
return $wpdb->get_var($query_course);
}
You will need to either know the user_id or get it from the post (sfwd-quiz, sfwd-course, sfwd-lesson) -see below.
The data you want can be all (*) You will have to do a meta_query if you want deeper data that is not in the post_type tables.
/**
* Gets the author of the specified post. Can also be used inside the loop
* to get the ID of the author of the current post, by not passing a post ID.
* Outside the loop you must pass a post ID.
*
* #param int $post_id ID of post
* #return int ID of post author
*/
function wpso49370180_get_author( $post_id = 0 ){
$post = get_post( $post_id );
return $post->post_author;
}
how i can get the users nice name in a function?
currently i got it with an avatar, but dont know hot to get the users nice name:
$table = $wpdb->prefix . "thumbsup_info";
$result = $wpdb->get_results("SELECT userid FROM $table WHERE postid = '$postid' LIMIT 10", ARRAY_A);
$total_user = $wpdb->num_rows;
for($i=0; $i <$total_user ; $i++)
{
$userid.= get_avatar($result[$i]['userid'],100);
}
return $userid;
Get the user object with the following line of code:
$user = get_user_by( 'id', $result[$i]['userid'] );
Then you can just access the property you need. To get the nicename for example you would use $user->data->user_nicename