How to modify post title in wordpress - php

I have 20 thousands plus posts and all posts have title started with
Watch etc etc...
I want to remove watch from all post title please give the solution how i can do this. Is there any filter which can do that.

Run this query in your phpmyadmin
UPDATE wp_posts
SET post_title = REPLACE(post_title, ' Watch ', '');
Where
wp_posts is your post table name

The simplest solution would be to use the following plugin and search and replace given string in the post_title column in the database.

Related

Remove duplicate posts from wordpress

There are posts which inserted multiple time with post_name like "icegram-2", "icegram-3", "icegram-4" along with "icegram".
Now, I want to remove posts with post_name "icegram-2", "icegram-3".
It's an example. There are more posts like this. I want to delete all such duplicate posts.
Is it possible with mysql query?
You could use the following plugin:-
https://en-gb.wordpress.org/plugins/delete-duplicate-posts/
Maybe have a look at How to delete Duplicate posts in WordPress
If I find anything else I will update my answer.
Delete Duplicate Posts plugin can helpful for you

Replace [keyword] in post_content with words from post_title MYSQL

I'm on a bit of a rampage today as I have been trying to literally weeks to get my website fully functional.
I have 500 posts or so with the heading "best hotel deals in XXXXX" and I have a template in each post_content row in the table.
Inside the template are words like [keyword], and I want to replace the "[keyword]" with "XXX" from the title.
Is this possible?
So, for example:
"post_title = Best hotel deals in London"
post_content = We have the best hotel deals in [keyword]"
I want to replace [keyword], with London." within the post_content.
I also have some search criteria for the certain posts I want to be affected, and they are the following:
SELECT *
FROM wp_posts
WHERE post_title LIKE 'best hotel deals in%'
AND post_status LIKE 'draft'
Any suggestions?
first, you need to extract the title keyword from post_title using php regex with pattern like '/best hotel deals in (.+?)$/i' use $ if title keyword goes to the end of line.
and then, you use php str_replace to replace [keyword] with your keyword found in the first step
as for your search criteria question, I don't understand your question, please elaborate.
You can do the update for a single keyword in SQL like this:
update
wp_posts
set
post_content =
concat(
substr(post_content, 1, instr(post_content, '[Keyword]') - 1),
substr(post_title, instr(post_title, 'Best hotel deals in ') + 20),
substr(post_content, instr(post_content, '[Keyword]') + 9)
)
where
post_title like 'best hotel deals in%' and
post_status = 'draft' and
post_content like '%\\[keyword\\]%'
Note, however, that if you're dealing with multiple keywords, this is going to get messy in a hurry. You are much better off using WordPress' built-in templating tools, or one of the many available WordPress plug-ins, rather than doing extensive manipulation directly in the database. Square peg, round hole, and all of that.
I ended up using this:
update wp_posts
set post_content = REPLACE(post_content,'[keyword]',trim(SUBSTR(post_title, 21)))
where post_title like 'best hotel deals in%' and post_status = 'draft'`
Worked like a dream.
Thanks everyone!

Selecting most recent result from MySQL where there are more than 1 rows matching ID

I'm using wordpress with some custom fields, to cut this short, im echoing out all rows that have the meta_key of post_author that arent empty and grouping likewise names together otherwise i might get Pete, Steve, Pete - when all i need is Pete, Steve. Below is the SQL that makes that work
$wpAuthors = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT(meta_value)
FROM $wpdb->postmeta WHERE meta_value !='' AND meta_key = 'post_author'
ORDER BY meta_key ASC"));
That works great. But what if i change the author FROM pete TO steve? Of course wordpress makes a duplicate entry for that post id, with a different Custom field row id. (Post ID on both rows would be something like 1100 while entry rows might be 3000 and 3001). Now the issue here is that my script still works, because both Pete and Steve are entered into the database in the correct column, but what i need it to do now is to select the most recent copy of that post id row to get the most recent author, does that make sense? I hope so!
define('WP_POST_REVISIONS', false); Add this variable in to wp-config.php, it will prevent wordpress to create post revisions.
After that install this plugin http://wordpress.org/plugins/wp-optimize/screenshots/.
Now remove all post revisions using above wp-optimize plugin.

How to format/clean name of posts in my wordpress blog

I have a wordpress blog with 2000 posts. Name of each post is a person's name. But the first name and last name are separated by a '-', I want to automate the process of removal of these '-' from the post names.
Example -
Present name of a post: Isaac-Newton
Desired name : Isaac Newton
Is there any script, or code to do it, as it will be a tedious job, If I have to manually edit the names of all the posts. Any help, suggestions what might work?
You could open the WP database in PhpMyadmin and run something like the following:
update wp_posts set post_title = replace(post_title, "-", " ") WHERE post_title LIKE '%-%';
Obviously backup the database first, to ensure you don't completely mess it up.

how wordpress can un-slug a title

i still , don't understand , how wordpress can understand what is this url refer to :
www.mysite.com/about-me/
they are using no identifier
if they using slug functions so how they can retain story information or in other word , how they change back the slugged title to select from database
It processes the "pretty" URL and queries the database with that data. Of course slugs are checked to be unique on creation. Details are in the function url_to_postid() in the file wp-includes/rewrite.php.
If you want to directly obtain the id from the slug you can query the database:
SELECT ID
FROM wp_posts
WHERE post_name = '$slug'
you might need to check wp_posts which is the default name, but it depends on the installation.
This is just a guess:
My guess is that they store the titles in a database, and make sure every title is unique. That way, they can do a look-up by title and know which item is coupled to that.

Categories