Wordpress Mysql query to replace empty alt img with post_title - php

I have several empty img alt on my wordpress site (about 3k).
After considering different solutions, looks like the best approach is to do a replace on the database.
I would like to replace empty alt in post_content (alt="") with post_title (alt="This is the post title").
Something like that:
update wp_posts
set post_content = REPLACE(post_content,'alt=""', 'alt=" + post_title + "')
where post_status = 'publish';
But I don't get how to insert the post_title value in the query.
Thank you for your help

use a query like this:
UPDATE wp_posts
set post_content =
REPLACE(post_content,'alt=""', CONCAT('alt="', post_title, '"'))
WHERE post_status = 'publish';
and for a specific lenght use SUBSTRING()
UPDATE wp_posts
set post_content =
REPLACE(post_content,'alt=""', CONCAT('alt="', SUBSTRING(post_title,1,20), '"'))
WHERE post_status = 'publish';

Related

Wordpress usermeta from db returns null

Hi im trying to get some custom content from the database. I tryed in phpMyadmin:
SELECT DISTINCT meta_value FROM wp_usermeta WHERE meta_key = 'company'
Which returns the correct content.
Im now trying to get the content in my plugin. But its returning null
global $wpdb;
$companys = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_usermeta WHERE meta_key = 'company'");
var_dump($companys);
Im new to sql, and googled for hours, but with no result...
You just need to use DISTINCT to your SQL query, something like:
global $wpdb;
$companys = $wpdb->get_results("SELECT DISTINCT(meta_value) FROM $wpdb->usermeta WHERE meta_key = 'company'" );
var_dump($companys);

Search from all custom-fields very slow. I need filter search for custom post type

I'm doing a custom search in my WP and in the search field, any typed word will be sought in custom-fields, in the titles and posting content.
It turns out that my database has more than 1 million custom-fields lines, and wanted to limit the search only for a certain type of post.
I already do this in the Loop to display the results, but I wish this filter was made in consultation because the page is taking over 20 seconds to display the result.
Can anyone help me? Below the code I'm using.
// SEARCH FROM ALL CUSTOM FIELDS
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
ALTER TABLE wp_posts ADD INDEX (postmeta)
SELECT DISTINCT post_id FROM {$wpdb->postmeta}
WHERE meta_value LIKE '%s'
", $keyword ) );
// SEARCH FOR TITLE AND CONTENT
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
ALTER TABLE wp_post s ADD INDEX (posts);
SELECT DISTINCT ID FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
OR post_content LIKE '%s'
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );
UPDATE:
Try this one. It's not tested but I'm sure it will work if you check and fix it a little bit:
// SEARCH IN title, content and meta_value
$post_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT ID FROM {$wpdb->posts} AS p,
LEFT JOIN {$wpdb->postmeta} AS m,
ON m.post_id = p.ID
WHERE p.post_type = '%s'
AND (
post_title LIKE '%%s%'
OR m.meta_value LIKE '%%s%'
OR post_content LIKE '%%s%')
", $post_type, $keyword, $keyword, $keyword) );
Note:
Assume you have taken input $post_type & $keyword. Eg: $post_type = "fairs";
I added % before and after %s to make sure it's widecard search.
In this case, I use DISTINCT because the joint table have repeated IDs. My apologies for the previous fault assumption on your first query that it was not needed.
Speed is not guaranteed, since you are doing a widecard search on post_content which is not indexed.
You should make sure the meta_value field is indexed for a little faster speed. If you meta don't store too much text in it.
GOOD LUCK!
ORIGINAL:
Firstly, let's clarify these:
Remove these 2 lines as it won't help (it make it worse due to repeated call):
ALTER TABLE wp_posts ADD INDEX (postmeta)
And
ALTER TABLE wp_posts ADD INDEX (posts);
ID is already distinct. So it won't help you do: SELECT DISTINCT ID
The final queries should look like these:
// SEARCH FROM ALL CUSTOM FIELDS
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
SELECT post_id FROM {$wpdb->postmeta}
WHERE meta_value LIKE '%s'
", $keyword ) );
// SEARCH FOR TITLE AND CONTENT
$post_ids_post = $wpdb->get_col( $wpdb->prepare( "
SELECT ID FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
OR post_content LIKE '%s' // Remove this will boost performance by 90% because post_content is not indexed
", $keyword, $keyword ) );
$post_ids = array_merge( $post_ids_meta, $post_ids_post );
What I recommend is to use this plugin: https://wordpress.org/plugins/search-everything/
Assuming that $keyword has no wildcards, you might try expressing this query as:
SELECT ID
FROM {$wpdb->posts}
WHERE post_title LIKE '%s'
UNION
SELECT ID
FROM {$wpdb->posts}
WHERE post_content LIKE '%s';
MySQL might figure out to use the indexes for this. The indexes that you want are:
create index wp_post_title_id on wp_post(post_title, id);
create index wp_post_content_id on wp_post(post_content, id);
This assumes that{$wpdb->posts} is really wp_post.

MySQLi query issue

With this request:
<?php
$query = '
SELECT
*
FROM
posts
WHERE
post_type = "post" AND
post_status = "publish" AND
post_content LIKE "%'.$search_string.'%" OR
post_title LIKE "%'.$search_string.'%"
';
I need to collect all data from the posts table, with the conditions above.
Since there were no issues, I will describe what I want to do. Imagine a record is actually a post, has a status publish, and there is a match in the request in either the post_content, or the post_title fields.
As a result, I get the last two operating conditions (check on compliance in the header). And all the rest are ignored.
Use this
$query = 'SELECT * FROM posts WHERE (post_type = "post" AND post_status = "publish") AND (post_content LIKE "%'.$search_string.'%" OR post_title LIKE "%'.$search_string.'%")';
Just combine your LIKE clause.
$query = 'SELECT * FROM posts WHERE
post_type = "post" AND
post_status = "publish" AND (post_content LIKE "%'.$search_string.'%" OR
post_title LIKE "%'.$search_string.'%")';
You need to combine OR that means combination of two column value.

Filtering the post_author outside the wordpress loop

I have a Mysql query set outside the wordpress loop, like the one below, which doesn't work. I would like to know how can I filter the sql results based on the author that created the post.
global $post;
$post_author = $post->post_author;
$sQuery = "SELECT DATE_FORMAT((post_date), '%M/%Y') 'Month',
FROM wp_posts p WHERE p.post_author = '$post_author'
Any assistance is appreciate it.
Thank you,
You have an extra comma after 'Month'. Try this:
$post_author = $post->post_author;
$sQuery = "SELECT DATE_FORMAT((post_date), '%M/%Y') 'Month' FROM wp_9691posts p WHERE p.post_author = '$post_author'";
I also added the final " to enclose the query string.

How can I grab data from all of the matching fields?

What is the propper syntax to grab data from all of the matching fields?
This example outputs only 1 of the matching fields:
$myvariable = "SELECT post_content
FROM wp_posts
WHERE post_name = 'testing'
AND post_status = 'publish'
AND post_type = 'post'";
echo = $myvariable;
You need to fetch an array of that query:
<?php
$query = mysql_query("SELECT post_content FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'");
while($result = mysql_fetch_array($query)){
echo $result['post_content'];
}
?>
That will loop through the result list from the query and echo the post_content field value.
EDIT: Wow... Same thing, a few seconds late. Ha!
Your question is not clear to me. There may be two cases:
You want to get all the rows which match you condition.
then you should use a loop to grab all matching records as below:
$result = mysql_query("SELECT post_content FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'");
if(mysql_num_rows($result)>0)
while($row = mysql_fetch_assoc($result)){
echo $row['post_content'];
}
I couldn't understand what you want to know. If you want to select all the fields of selecting row then use:
SELECT * FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'
OR
SELECT col1,col2,...,coln FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'
If you want to check conditions with all the fields then you are on the right track, just compare each value with its respective column.
list all the fields in your select statement or use * to select all.
$myvariable = "SELECT * FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'"
You'll need to pull out the query and run a loop over the result rows that come back.
$query = mysql_query("SELECT post_content FROM wp_posts
WHERE post_name='testing'
AND post_status='publish' AND post_type = 'post'");
You'll need to adjust the above query to whichever rows you want to display as well as whatever criteria you want to match against.
And then run something like a while loop with each pass of the rows that were returned:
while($result = mysql_fetch_array($query))
{
echo $result["post_content"];
}
The above will go and pull out all the rows one by one that matched your query and display them however you style it.
If you want to match any, then your SQL is false.
SELECT post_content FROM wp_posts WHERE post_name = 'testing' OR post_status = 'publish' OR post_type = 'post'
The query above finds each record which contains exactly one of the search strings.
But if you want to find it within a sentence then you better use the MATCH function.
MySQL Full-Text Search Functions

Categories