Performance bug - slow DB query

Since 2017-09-26 I have terrible problems with performance on my website. Loading single post or any other page takes even 20s. I've been debugging it it found out, that the problems are DB queries. For example this one is executing almost with every page load and takes about 36s: SELECT t.*, tt.*, tr.object_id FROM az2_terms AS t INNER JOIN az2_term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN az2_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('znacka', 'model') …
Category: Web

Fix wp_term_relationships slow query in get_posts

I used the Query Monitor plugin to find slow query on my WordPress site. The below-mentioned WP_Query->get_posts() takes about 0.3446 query time out of the total Database Query time of 0.3976. SELECT wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND wp_posts.ID NOT IN (203598) AND ( wp_term_relationships.term_taxonomy_id IN (17) OR wp_term_relationships.term_taxonomy_id IN (11652,20693,21952,23971,24907,24908,25928) ) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 6 I'm guessing it …
Category: Web

How to fetch Data in WordPress using MySQLi or $wpdb

I have custom table like this: useraw 1. id (Primary*) 2. user_ip 3. post_id 4. time I am inserting data in the table using $wpdb->insert($table_name , array('user_ip' => $user_ip, 'post_id' => $postID, 'time' => $visittime),array('%s','%d', '%d') ); There are four rows I inserted using this code: id : 245 user_ip : 245.346.234.22 post_id : 24434 time : 255464 id : 345 user_ip : 245.346.234.22 post_id : 23456 time : 23467 id : 567 user_ip : 245.346.234.22 post_id : 57436 time …
Category: Web

Database issue?

I installed WordPress from a zip file via FTP, followed the install procedure exactly - When accessing the URL, all I could see is a default page without opening any install script. I then manually entered https://"mysite".com/wp-login.php and it opened a login window. I entered the username and PW I created in wp-config.php - I was advised no such user- double checked everything and found no errors I could determine. I'm guessing that the problem is something in either database …
Category: Web

Unserialize Custom Field & Save as Multiple Rows in Wordpress Database

I have a custom field in post_meta table of wordpress database. It's called 'combined'. I want to seperate the values based on key and save them as multiple rows if there are multiple values for the key. My meta_key: Result after I run my code: What I want: My code: add_action( 'init', function() { if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) { return; } $query = new WP_Query( [ 'no_found_rows' => true, 'update_post_term_cache' => false, 'fields' => 'ids', …
Category: Web

Strange characters on wordpress site - Not UTF8 Issue

I've imported WordPress post data onto a new site and noticed I have strange characters showing on pages and Blog posts. It usually shows where apostrophes should be. I've searched multiple solutions to UTF8 & latin1 solutions with success. I've looked at my database and the characters are showing there too.
Category: Web

How can I trust switch_to_blog()?

When I call switch_to_blog() with a blog id, I don’t know whether that blog actually exists. The function returns always true. Test case: switch_to_blog(PHP_INT_MAX); $post = get_post(1); restore_current_blog(); This will result in database errors that are exposed to the user. How can I prevent that? Real-world use case I was the lead developer of MultilingualPress. When a user translates a post, she gets a screen like this: Now the following can happen: She saves the post successfully and continues translating …
Category: Web

Ajax not working to insert, query and result data

On my site, through a form I send/register same information in database, do a SELECT/query and return it! Return the last table saved in database, just that user just entered on the form (along with some more information coming from the database). How I want to display these values coming from databse in a modal bootstrap it's necessary that the page doesn't give the refresh. For this, I inserted the AJAX as follows: $(document).ready(function(){ $('#enviar').click(function(){ $.ajax({ //CAAL AJAX IN WORDPRESS …
Category: Web

Best db table structure for users with many records

I want to allow users to have 1000+ records in my WordPress system. I was wondering is it best to have just one table for all users and their records or one table per user? Keep in mind that I could have 1000 users. A user columns for records would look like: User_client_name | User_id | User_post_id | etc... Each user might have 1000+ records in the database of their contacts. If I put all users in one table, I …
Category: Web

Serialize data before inserting into the DB

I need to store some post IDs in the DB in a custom table/field. The IDs will have to be stored in a serialized format. I know that, ie. for the options, serialization is done automatically for add/update functions (add_option(), update_option()). Is there a built-in WP function to serialize data for any other case? Or should I just use serialize()? Similarly, as soon as the data will be retrieved, is there a stock WP function to unserialize it, or will …
Category: Web

Searchable database of members using multiple criteria / filters

I'm very new to wp let's say that first! What I need is to create a site which will list a database of members of an organisation. Users will also be able to sign-up and add themselves to the database. But additionally I want users to be able to search for other users using given criteria check boxes, for example: Is male [ ] Is female [ ] Lives in city x [ ] Lives in city y [ ] …
Category: Web

How to set up hierarchical relationships without using plugins / meta query

I'm running a large database of musical artists and their releases. I inherited the site after it running for a long time. There are currently several different custom post types, created with a Toolset plugin. The post types are hierarchical like this Artists | Directors | Releases | Albums | Singles | Videos | Misc | Artists and Directors are parent to Releases, which is a parent to the Version Albums | Singles | Videos | Misc | are the …
Category: Web

Catchable fatal error in formatting.php

I'm running a WordPress 4.4.2. I have one site that's working fine. When I went to set up another site, I got the following error upon trying to save the "general" settings page: Catchable fatal error: Object of class WP_Error could not be converted to string in /{site-url}/wp-includes/formatting.php on line 1025' Before saving the settings, I deleted the existing sample page and sample post and created a new page. Didn't touch anything else. After researching the problem, I have done …
Category: Web

How To Save Data From Remote APIs To ACF?

I've been attempting to ingest remote data from an api partner into Wordpress. To data I've used wpallimport plugin, but it's quite limited. How would one connect with a remote api and then save the data to a an ACF field either one time or periodically? I've test out wp_remote_get($apiUrl); and even created a POC but saving the data to the WP database has not been successful yet.
Category: Web

import (migration) user database to wp-users

I am having hard time for importing to user-meta and post-meta from my non-wp-tables. I am trying to do db migration from user table and post table to WordPress tables. So it might be user table to wp-users and wp-usermeta post table to wp-posts and wp-postmeta I have been trying to find good plugins but it's all limited. (only basic information) My old user table has all of users information on only 1 row (1 line) such as phone number, …
Category: Web

WP_Options ID high

I have a site where the options_id field in the wp_options table has grown very, very large. A month and a half after the installation, the auto_increment value is now at over 10 million. While there are a number of plugins installed (such as ACF and W3 Total Cache) they do not seem to be the cause. They have been installed on others sites together with the same theme framework without any issues. I have a local dev environment of …
Category: Web

WordPress is not creating table in database after activating plugin,

I struggled with this a long time! Can anyone help me? My plugin is not creating a database table once activated. The following code is in the main plugin file. function woocq_activate() { global $wpdb; $table_name = $wpdb->prefix . 'woocq'; $sql = "CREATE TABLE $table_name ( id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, name VARCHAR NOT NULL, message TEXT NOT NULL, question_id INT NOT NULL, product_id INT DEFAULT NULL, timestamp BIGINT NOT NULL, PRIMARY KEY (id) );"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( …
Category: Web

Wordpress showing install screen after max_questions error

I am working in a PHO environment in Heroku with ClearDB. At some point (really low), I get this error: WordPress database error User 'XX' has exceeded the 'max_questions' resource (current value: 3600) for query SHOW TABLES LIKE 'wp\\_users' made by display_setup_form. I guess this is an error due to use the free database plan of ClearDB. However, after getting that error (spotted in the server logs), the server is redirecting to the install screen. Is there any way to …
Category: Web

wpdb get_results() returns only 2 rows

I am trying to create custom admin widget to present how many users has registered within last days. I have created function as below: function mdbootstrap_add_dashboard_recent_users(){ global $wpdb; $query = " SELECT count(*) as counter, DATE(wp_users.user_registered) as regdate FROM wp_users GROUP BY DATE(wp_users.user_registered) ORDER BY DATE(wp_users.user_registered) desc"; $posts = $wpdb->get_results($query,OBJECT); print_r( $posts); } However for some reason it's returning only 2 rows: Array ( [0] => stdClass Object ( [counter] => 1 [regdate] => 2016-01-12 ) [1] => stdClass Object …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.