basic information

  • There is a website on wordpress.
  • The site has entries with the headings of the format "Alex 0981178989 Ford Mondeo 2006 onwards."

  • In the folder / wp-content / uploads / 2018/09 / there are images with the name of the phone number, that is, 0981178989.jpg

  • Images are displayed in Media files, that is, they have a unique ID and have the type of post attachment in the database.

  • The title of the images corresponds to the phone number (in this case 0981178989)


What do we have

1. We have a SQL query, which we get the ID of the post, in the header of which there is the number 0981178989

SELECT ID FROM wp_posts WHERE post_title LIKE '%0981178989%' AND post_type = 'post' 

2. We have a SQL query with which we get an ID attachment (images 0981178989.jpg with post_type = 'attachment')

 SELECT ID FROM wp_posts WHERE post_title LIKE '%0981178989%' AND post_type = 'attachment' 

What do we need

In SQL query

 UPDATE `DB_Name`.`wp_posts` SET `post_parent` = 'XXXXX' WHERE `wp_posts`.`ID` = YYYYY 

instead XXXXX insert the result of the first query (numeric value)

instead of YYYYY insert the result of the second query (numeric value)

  • everything can be done through requests - Yaroslav
  • UPDATE таблица, (первый подзапрос), (второй подзапрос) SET ... although everything can be crammed into one subquery. - Akina

1 answer 1

Example:

 UPDATE `DB_Name`.`wp_posts` SET `post_parent` = (SELECT ID FROM wp_posts WHERE post_title LIKE '%0981178989%' AND post_type = 'post') WHERE `wp_posts`.`ID` = (SELECT ID FROM wp_posts WHERE post_title LIKE '%0981178989%' AND post_type = 'attachment');