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.
Related
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';
I want to create a search page for my website, but my SQL query doesn't return what I want.
This is my SQL query
SELECT * FROM blog_posts WHERE post_content LIKE '%" . Addslashes($_GET['s']) . "%' AND post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC lIMIT 0, 4;
But it only return the published posts, it doesn't return the searched post
This is my search page : https://lite.the-scientist.fr/search
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.
I'm using wp-ecommerce plugin for my own online ordering bakeshop (http://www.kingsbakery.mpkproducts.com/)
I don't want the way how customers made an order. I want similar order form like this (https://www.ssfamousdeli.com/order_ship.aspx).
I run a custom query in Mysql and I can only retrieve the product title and description. I use this sql statement:
<?php
$sql = "SELECT * FROM wp_posts WHERE
post_type like 'wpsc_product' AND
post_status like 'publish'
ORDER BY ID Asc";
$query = mysql_query($sql);
?>
I only stop there coz I don't know how to join wp_postmeta to select the price.
Please help. Thanks
<?php
$sql = "SELECT wp_p.*, wp_pm.meta_value FROM wp_posts wp_p, wp_postmeta wp_pm WHERE
wp_p.post_type like 'wpsc_product' AND
wp_p.post_status like 'publish' AND
wp_p.ID = wp_pm.post_id
wp_pm.key = 'Price' --This should be the meta key
ORDER BY wp_p.ID Asc";
$query = mysql_query($sql);
?>
Hope this will be of help.
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