wordpress database table name update - mysql

I am trying to read this error log so I can locate and update whatever is trying to find wp_posts and update it to wp_oeai_posts but cant for the life of me find any code looking for wp_posts in the db or the php
WordPress database error Table 'testdb.wp_posts' doesn't exist for query SELECT u.* FROM uploads AS u LEFT JOIN approvals AS a ON a.uploadID = u.ID
left join wp_posts as p on p.id = u.postid order by case when p.post_status = 'pending' then 0 else 1 end, submittedat desc made by require('wp-blog-header.php'),
require_once('wp-includes/template-loader.php'), include('/themes/twentyfourteen/page.php'),
get_template_part, locate_template, load_template,
require('/themes/twentyfourteen-child/content-page.php'),
the_content, apply_filters('the_content'), WP_Hook->apply_filters,
do_shortcode, preg_replace_callback, do_shortcode_tag, user_upload_manager_listsubmissions,
user_upload_manager_submissions->printsubmissions

Found the code that needed to be updated, listsubmissions.php
' left join wp_oeai_posts as p on p.id = u.postid'

Related

Is it possible to add multiple on clauses inside an INNER JOIN clause?

I have a query that works perfectly, however I need to change it a bit but it shows me an error and I can't figure out why. Below is the code before and after the changes I made:
BEFORE:
SELECT *,
(SELECT GROUP_CONCAT(pho_file_name) FROM post_images WHERE pho_post_id=posts.ID) AS photo_file_array
FROM users
INNER JOIN posts ON users.Id = posts.post_author
ORDER BY posts.ID;
AFTER:
SELECT *,
(SELECT GROUP_CONCAT(pho_file_name) FROM post_images WHERE pho_post_id=posts.ID) AS photo_file_array
FROM users WHERE users.Id = "1"
INNER JOIN posts ON users.Id = posts.post_author ON posts.post_date = "2020-12-04 07:51:21"
ORDER BY posts.ID;
It shows me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INNER JOIN posts ON users.Id = posts.post_author AND posts.post_date "2020-12...' at line 4
I'm a newbie on MySql but from what I can understand I think the error occurs because of the the double ON inside the INNER JOIN. So, is it possible to add multiple ON inside the INNER JOIN? Thanks in advance!!
You have a few syntax issues, you can't put joins and where anywhere, you also need to use the correct delimiters and data types.
Try the following and note using table and column aliases makes for an easier-to-read query.
Additionally, consider not using select * and reference only the columns you actually require, if possible.
SELECT u.*, p.*, (
SELECT GROUP_CONCAT(i.pho_file_name)
FROM post_images i
WHERE i.pho_post_id = p.ID
) AS photo_file_array
FROM users u
JOIN posts p ON p.post_author = u.Id
AND p.post_date = '2020-12-04 07:51:21'
WHERE u.Id = 1
ORDER BY p.ID;
Here is a full working query. The errors (double ONclause, WHERE clause in the wrong position, wrong quotes) are corrected. Moreover, the ID is compared to an integer now and the post_date to a timestamp literal. I've used table aliases to get this more readable.
SELECT
u.*,
p.*,
(
SELECT GROUP_CONCAT(pi.pho_file_name)
FROM post_images pi
WHERE pi.pho_post_id = p.id
) AS photo_file_array
FROM users u
INNER JOIN posts p ON p.post_author = u.id
AND p.post_date = TIMESTAMP '2020-12-04 07:51:21'
WHERE u.id = 1
ORDER BY p.id;
As to the tables: I suggest you are more consistent with your column names. Why do you call the post ID post_author? One would assume a name here. Just call it post_id in every table. And you don't have to precede columns with abreviations like pho. Just qualify all columns with their tables like I did in my query.

Table MYSQL query

I have used WPAllImport to import data from a CSV-file to Advanced Custom Fields.
I now want to put them back together with a SQL query, but dont know how to do it.
I've tried WPDataTables, but when I choose 5 or more tables, WPDataTables stops.
If I pick 2, I get this code
SELECT posts_podukter.post_title AS podukter_post_title,
podukter_meta_produkter_0_pris_tbl.meta_value AS podukter_meta_produkter_0_pris
FROM beta_h3L_posts AS posts_podukter
INNER JOIN (SELECT podukter_meta_produkter_0_pris_tbl_posts.ID as id, meta_value, meta_key FROM beta_h3L_postmeta AS podukter_meta_produkter_0_pris_tbl_postmeta INNER JOIN beta_h3L_posts AS podukter_meta_produkter_0_pris_tbl_posts ON podukter_meta_produkter_0_pris_tbl_postmeta.post_id = podukter_meta_produkter_0_pris_tbl_posts.ID AND podukter_meta_produkter_0_pris_tbl_posts.post_type = 'podukter') AS podukter_meta_produkter_0_pris_tbl
ON podukter_meta_produkter_0_pris_tbl.meta_key = 'produkter_0_pris' AND podukter_meta_produkter_0_pris_tbl.id = posts_podukter.ID
WHERE 1=1
AND posts_podukter.post_type = 'podukter'
I think this is too much code.
Can someone help me to get on the right way.... :-)
This is what the table should look like
Here is a capture how the table should look like
I would agree that this is "too much code" which sounds sort of ridiculous, but in this case totally applies. That SQL statement that was produced could be written as:
SELECT
post.post_title as podukter_post_title,
postmeta.meta_value as podukter_meta_produkter_0_pris
FROM beta_h3L_posts AS posts
INNER JOIN beta_h3L_postmeta AS postmeta
ON postmeta.post_id = post.ID
AND postmeta.meta_key = 'produkter_0_pris'
WHERE posts.post_type = 'podukter'
If there is another metavalue that you need you can join again to your meta table:
SELECT
post.post_title as podukter_post_title,
postmeta.meta_value as podukter_meta_produkter_0_pris,
postmeta2.meta_value as tilbudspris
FROM beta_h3L_posts AS posts
INNER JOIN beta_h3L_postmeta AS postmeta
ON postmeta.post_id = post.ID
AND postmeta.meta_key = 'produkter_0_pris'
INNER JOIN beta_h3L_postmeta AS postmeta2
ON postmeta.post_id = post.ID
AND postmeta2.meta_key = 'tilbudspris'
WHERE posts.post_type = 'podukter'
I don't know what any of these words mean (besides post and postmeta) so I'm just going to assume that this is right/helpful.
The only thing is that you may want to switch to using a LEFT OUTER JOIN to your postmeta table just in case the meta_key you are after doesn't exist for the post.id you are querying. In that case, with an INNER JOIN the id/post will be dropped from the result set where a LEFT OUTER JOIN will show the id/post record with a blank for whatever that corresponding meta_value is that you are joining in.

MySQL using 'not in' list with wildcards issue

i am writing the following SQL statement using my existing wordpress db as a learning environment...
Select
wp.ID,
wpum.user_id,
wpp.post_name
From
wp_users As wp Left Join
wp_usermeta As wpum
On wp.ID = wpum.user_id Left Join
wp_posts As wpp
On wpum.user_id = wpp.post_author
Where
wpp.post_status = 'publish' and
wpum.meta_key = 'nickname' and
wpp.post_name not in ('sample%', 'hello%')
order by
wpp.ID desc
As you can see I have used alias's for my tables and filtered my results of the word press posts to be only 'published' posts and only selecting the nickname to avoid duplicate results by user_id.
My issue is the wildcards do not appear to do anything.. my results are as returned with the sample & header posts still showing.
However, this would work as:
Select
wp.ID,
wpum.user_id,
wpp.post_name
From
wp_users As wp Left Join
wp_usermeta As wpum
On wp.ID = wpum.user_id Left Join
wp_posts As wpp
On wpum.user_id = wpp.post_author
Where
wpp.post_status = 'publish' and
wpum.meta_key = 'nickname' and
wpp.post_name not like 'sample%' and
wpp.post_name not like 'hello%'
order by
wpp.ID desc
My output is with the desired results removed.. anybody able to help me out? The second example is perfectly usable... I would just like to know my alternatives to learn good practices.
I've tried googling and using a few examples, but not having much luck.
Thanks all
Apol's if my post is wrongly formatted or this has been answered before, again I have searched!

MYSQL - Inner Join Syntax Error

Yeah, little confused here. I have number of tables in one of my wordpress installations and the name of them and the field names are pretty much descriptive. So I don't think that you need any explanation for them. My problem here is actually with the forth inner join I am trying to do. The query is simple:
SELECT *
FROM wp_posts AS post
INNER JOIN wp_postmeta AS postmeta ON post.ID = postmeta.post_id
INNER JOIN wp_game AS game ON post.ID = game.post_id
INNER JOIN wp_game_platform AS platform ON game.game_id = platform.GameId
INNER JOIN wp_platform_release AS release ON release.PlatformID = platform.ID
WHERE post.post_type = 'games' AND postmeta.meta_key = 'post_views_count' AND platform.Status = 0 AND release.ReleaseDate > 1435917562
ORDER BY postmeta.meta_value DESC, game.game_id
LIMIT 100
This is supposed to get list of all the game platforms (Xbox version, PC version of a game) that are not released yet and sort them with their page visits. As you can see the page visits are stored as post meta so I did a join on wp_postmeta. The above query gives me this syntax error:
failed : You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'release ON release.PlatformID = platform.ID WHERE post.post_type
= 'games' AND ' at line 1
Which clearly indicated that there is a syntax error in the query that I can't see. Funny enough if I run the following query, it returns the expected results (all the games [released or not] sorted by their post visits):
SELECT *
FROM wp_posts AS post
INNER JOIN wp_postmeta AS postmeta ON post.ID = postmeta.post_id
INNER JOIN wp_game AS game ON post.ID = game.post_id
INNER JOIN wp_game_platform AS platform ON game.game_id = platform.GameId
WHERE post.post_type = 'games' AND postmeta.meta_key = 'post_views_count' AND platform.Status = 0
ORDER BY postmeta.meta_value DESC, game.game_id
LIMIT 100
At first I thought that there is a JOIN limit but after research I found out that the limit is more than 60 tables which is far from 5. So you see any thing that I can't?
Thanks
release is a reserved word - try changing the name of the alias to something else. See : https://dev.mysql.com/doc/refman/5.0/en/keywords.html

Getting posts from wordpress returns the same result when present in several cathegories

In the home page of a website I need to return the last 3 posts of the blog, created with wordpress.
I have this code:
SELECT p.post_title, p.post_date, p.post_content, wpr.object_id, dt_blog_terms.name, dt_blog_terms.slug
FROM dt_blog_terms
INNER JOIN dt_blog_term_taxonomy ON dt_blog_terms.term_id = dt_blog_term_taxonomy.term_id
INNER JOIN dt_blog_term_relationships wpr ON wpr.term_taxonomy_id = dt_blog_term_taxonomy.term_taxonomy_id
INNER JOIN dt_blog_posts p ON p.ID = wpr.object_id
WHERE taxonomy = 'category'
AND p.post_type = 'post'
AND p.post_status = 'publish'
AND slug != 'notizie-notifiche'
ORDER BY `post_date` DESC
LIMIT 3
As you can see, I have one cathegory (notizie-notifiche) that I want to exclude. This sql string works, but I have a problem when the post has more than one cathegory. In this case, it is returned once for every cathegory, while I want to show it only once in total.
Any idea?
I thought I could use DISTINCT, but it does not seem to work with this kind of SELECT statement.
I can tell you what you need to do, but I can't modify the query to do it. You use the term "category" in your question, but there is no field by that name in your data.
You need to aggregate your data, at the level you want, and then include a having clause. The following group by may solve your problem:
group by p.id
having sum(case when slug = 'notizie-notifiche' then 1 else 0 end) = 0