Updating term_relationships with new categories from CSV - mysql

I am trying to update term_relationships with new categories established from a CSV, and am not too familiar with SQL.
I have already uploaded the CSV to the database and created a table called ut_csv, this table has several columns but the important ones are the ID and the category. The ID is the specific posts ID and the category already has the term_taxonomy_id.
I ran two scripts, the first being:
INSERT INTO wp_term_relationships
(object_id,term_taxonomy_id,term_order)
SELECT ID,category,0 FROM ut_csv
where post_type = 'post'
this failed with the error of #1054 - Unkown column post_type in where clause
So I ran the script, but dropped the where post_type = 'post, and this ran a success but when I checked the wp_term_taxonomy the 5 categories had no count.
Again I am unfamiliar with SQL so I may be doing something wrong or completely stupid. Any advice / help would be great.
Thanks

Related

Query a table based on values from within that table [duplicate]

This question already has answers here:
Using Substring_Index with data from a second table
(3 answers)
Closed 2 years ago.
We have a WordPress site that contains a Q&A plugin, that needs to be moved to a different hosting. The Q&A posts only make up for a fraction of the total Posts table, so I want to filter them out using a SQL query.
I want to select everything in the DB, through PhpMyAdmin, for the rows that match one of the following criteria:
post_type = "answer"
post_type = "question"
post_type contains revision, preceded by the ID of either one of the previous criteria. For example: 21-revision-v1 or 10903-revision-v1 Where I want to select those posts of which the first numerical part matches the ID of posts selected in the previous 2 requirements.
I am a complete novice to SQL so I started with some googling, and found the concept of Temporary Tables. Which lead me to create this bit of code:
SELECT * INTO #QA
FROM `wp_posts` WHERE
`post_type` = "answer" OR
`post_type` = "question"
However I get the following error:
#1064 - Er is iets fout in de gebruikte syntax bij 'FROM wp_posts WHERE
post_type = "answer" OR
post_type = "question" LIMI' in regel 2
Which translates to "There is somthing wrong with the syntax near"....
Is what I am attempting even feasible?
MySQL does not support the select ... into ... syntax to write to a table, as pinpointed in the documentation.
Instead you can use insert ... select:
insert into `#qa`
select *
from wp_posts
where post_type in ('question', 'answer')
The syntax that you are using is most commonly associated with SQL Server. MySQL uses the (more common) create table as syntax. And, it allows specifically for temporary tables in the syntax.
So, the equivalent in MySQL is CREATE TABLE AS:
CREATE TEMPORARY TABLE QA AS
SELECT p.*
FROM wp_posts p
WHERE post_type IN ('answer', 'question');
Note that a temporary tables is a very specific type of table that exists only in the current "session" -- say, your current connection to the database. It is not visible to other users and it will disappear when you reconnect to the database.
The SELECT * INTO... syntax is used to create a new table.
If this is what you want then the syntax for MySql is:
CREATE TABLE tablename AS
SELECT * FROM `wp_posts`
WHERE `post_type` = 'answer' OR `post_type` = 'question'

Wordpress SQL delete posts and their related meta

I have the current SQL query which will delete all posts from a custom post type clothing which are older than 2 days
delete
p,pm
from wp_posts p
join wp_postmeta pm on pm.post_id = p.id
where p.post_type = 'clothing'
and DATEDIFF(NOW(), p.post_date) > 2
The problem is that this query doesn't seem to delete the related metas such as related custom fields of the deleted posts.
My question is, how can I modify this code to also delete the relate metas from those posts?
Thanks
You should process in two steps :
1- with you query, build an array of posts to delete
2- loop this array with foreach and wp_delete_post()
(http://codex.wordpress.org/Function_Reference/wp_delete_post)
The core function wp_delete_post will take care of all related data, like metas, but also counts of posts in terms, wich are stored in the database and modified on insert or deletion of posts

SQL Query to delete WP post using date range on a MySQL database

I am migrating a news aggregation WP site to a commercial service. We currently have over 14,000 posts on it.
We want to save the current database and reuse it under a different domain name for historical purposes.
Once we move to the new site we want to trim the WP database of all posts and related tables that are older than 01.01.2013
I know how to do a simple select where delete query.
But WP forum mods have told me that I should do an inner-join on the following tables to ensure I get everything cleaned up:
wp_postmeta
wp_term_relationships
wp_comments
wp_commentmeta
I am not familiar with inner-join. Can someone help me with this?
Not completely understanding the table structures involved, an INNER JOIN will join one table to another and return the records that match based on a certain criteria (usually joining two fields together such as a primary key and a foreign key).
To delete records from one table where some or all are in another table, the following syntax would be used:
DELETE TableName
FROM TableName
INNER JOIN AnotherTable ON TableName.id = AnotherTable.id
Here's a good visual representation of JOINS.

SQL joining wordpress tables

I am struggling with an SQL query to join 3 tables to return specific results.
Anyone familiar with wordpress may be able to assist as I am using word press to power post interactions with the db behind the scenes but am building a custom UI. The three tables are: -
posts (**ID**, post_title, post_content, post_modified_gmt)
term_relationships (**object_id**, term_taxonomy)
terms (**term_id**, name, slug)
I have got as far as..
SELECT posts.post_title FROM posts
INNER JOIN term_relationships
ON posts.ID = term_relationships.object_id
Which returns a list of all the post titles that have a matching ID number in term_relationships. However term names are in the table 'terms' and the ID's don't match the other two tables. In the terms table the term_id refers to the name of the term, e.g
term_id = 2, name = blog
Basically I am trying to achieve a query whereby if I set the term_id = 2 it returns all the rows from the table posts that have the term relationship to blog, meaning the query returns all blog posts and I am completely lost!
Can anyone give me a few pointers? my mind is boggled.
I managed to achieve the result I was looking for with the following query, but would still appreciate some pointers should anyone stumble upon this and have the inclination :)
SELECT * FROM posts
INNER JOIN term_relationships
ON posts.ID = term_relationships.object_id
WHERE term_relationships.term_taxonomy_id = 2
AND posts.post_type = 'post'
AND posts.post_status = 'publish'
ORDER BY posts.post_modified_gmt ASC
LIMIT 0,5

'count' in wp_term_taxonomy without wp_term_relationships

I lost all my categories and tags in a Wordpress import and I'm trying to get them back. My 'wp_term_relationships' table is empty, which I assume is the problem. I do have a 'wp_term_taxonomy', which includes a "count" column, presumably how many posts are in each category or tag:
http://min.us/mv168n
If the database knows that count, is there any chance it knows which posts are in that count, or is that solely the territory of wp_term_relationships ? Is there anything I can do?
Yes all the relations between posts and categories are in 'wp_term_relationships' so unfortunately if you lost that table there is no way to know which post is in which category or taxonomy