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
Related
I want to add posts to a category and remove from another if conditions are met: regex matches content and is not in a certain category.
so in points:
Actions:
add posts to category A
remove those posts from category B
If Conditions:
post content matches certain regex
not in category C
I have two snippets of SQL code which I am having trouble combining.
---snippet 1--- add to category based on regex in post ------
REPLACE INTO wp_term_relationships (term_taxonomy_id, object_id) SELECT '141', ID
FROM `wp_posts`
WHERE (post_title REGEXP '((?<!\\b[A-Z]|\\b[A-Z][A-Z])\\$|\\bAU?\\$|CA?N?\\$)[^\\d\\r\\n]?((?:\\d{1,10}[,. ])*\\d{1,10})[ .]?(k)?|((?:\\d{1,10}[,. ])*\\d{1,10})[ .]?(k)? ?(?:USD?\\b|dollar|cash|\\bbucks\\b|GBP|EURO?S?\\b|AUD\\b|CAD\\b|INR\\b)'
OR post_content REGEXP '((?<!\\b[A-Z]|\\b[A-Z][A-Z])\\$|\\bAU?\\$|CA?N?\\$)[^\\d\\r\\n]?((?:\\d{1,10}[,. ])*\\d{1,10})[ .]?(k)?|((?:\\d{1,10}[,. ])*\\d{1,10})[ .]?(k)? ?(?:USD?\\b|dollar|cash|\\bbucks\\b|GBP|EURO?S?\\b|AUD\\b|CAD\\b|INR\\b)')
AND (post_type = 'post' )
---snippet 2--- remove posts from a category that exist in another category ------
DELETE FROM wp_term_relationships
WHERE term_taxonomy_id = 160 ##CategoryID
AND object_id NOT IN
(SELECT object_id FROM (SELECT * FROM wp_term_relationships) AS tr2
WHERE tr2.term_taxonomy_id = 161); ##OtherCategoryID
snippet 2 is all I could find close enough to what I want to do.
Cant seem to get it all working together in one statement
Another way to do it is with transactions. (You must use a table-architecture that supports them, and these days you probably do.)
A "transaction" is an atomic operation: either "everything happens at once" (COMMIT), "or nothing does" (ROLLBACK). Now, within the transaction, you can perform multiple queries to get the work done, and other users will not see the intermediate states. When you commit your transaction, they see the finished result appear instantaneously – unless they're in the middle of a transaction, too.
This is an intentionally simplistic explanation ... but you get the idea. Now you don't have to "get it all done in one query."
I am developing a social network iOS app using a PHP backend with a MySQL database. I have 3 tables in my database to store Posts, Comments and Likes.
Currently, the app requests up to 50 posts at a time which includes various specific data. I do this using a SELECT query from MySQL. My 'Likes' are stored in the 'Likes table' as 1 Like per row. This includes their username and which post they 'liked'.
When the app is refreshed, I need to gather the 'Likes' data on the posts that have just been loaded. My problem is that I do not know the best way to query MySQL to return all associated likes with that user. This means that when I load some posts, I want the user to see which posts they have already liked.
Here is a visual description of my problem:
Please understand that up to 50 of these posts will be shown at one time.
There is several way to do this and depending on your project you might want to optimise my answer.
I assume your Likes table has a structure of (int) id, (int) user_id, (int) post_id.
To answer directly to your question (about doing a query only for the refresh), the next MySQL query will give you the list of post_id liked by the user. You need to replace %user_id% by the id of the user and %post_ids% by the list of the 50 posts ids, separated by a comma:
SELECT post_id
FROM Likes
WHERE user_id = %user_id% AND
post_id IN (%post_ids%)
However, if the refresh implies the whole page (and not only an AJAX request or so on) I would consider implementing this directly in the query getting the posts. For example :
SELECT p.id, p.content, p...., l.id AS `Liked`
FROM Posts AS p
LEFT JOIN Likes AS l
ON p.id = l.post_id AND
l.user_id = %user_id%
WHERE ...
Please also consider adding indexes on the right columns.
As promised, my solution is here:
SELECT `posts`.`postnumber`, `posts`.`message`, `likes`.`didLike`
FROM `posts`
LEFT JOIN `likes`
ON `posts`.`postnumber`=`likes`.`post_id`
AND `likes`.`user_id`='USERNAME';
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
Let's say I have posts and categorizations.
Post(id)
Categorization(post_id, topic_id)
I'd like to fetch posts that don't belong to a specific topic id.
In my case I have to use an inner join when joining Post to Categorizations as i have other filters to execute.
How do I go about this?
I have tried the following:
Post.joins(:categorizations).where("categorizations.topic_id != ?", doomed_topic_id)
But this returns posts that still have OTHER topics. it only works with posts with just one single topic that happens to be the unwanted one.
For instance, if I have a post with 2 categories (the doomed topic_id AND another topic) this query fails and actually fetches it, instead of filtering it out.
Try:
Posts.where('not exists
(select * from categorizations
where post_id = posts.id and
topic_id = ?)', doomed_id)
I have two tables: posts and comments. The identifier column in posts is post_id. Each comment has also post_id to point to the responded post.
posts comments
- post_id - comment_id
- message - message
- time - time
- post_id
What I need: Select posts, and also the comments posted on each selected post.
What I do now: I have two queries.
First query to select posts, then I loop thru it, save all post_ids into an array.
Then second query to select all comments based on the saved post_ids from previous step.
Then I loop the comments and adds them to the posts variable.
What I want to do and don't know how: get posts and comments in one query.
Try with -
select * from posts join comments on posts.post_id = comments.post_id
Hope will do the trick.
You can use join concept. For more information see http://www.sitepoint.com/understanding-sql-joins-mysql-database.
Use join clause:
SELECT *
FROM posts AS p,
comments AS c
INNER JOIN posts ON p.post_id = c.post_id;