Woocommerce and Yoast product search error - wordpress

I've been trying to use product search in the admin area, it used to work fine, now i get this errors:
SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM mu05iq_posts posts
LEFT JOIN mu05iq_wc_product_meta_lookup wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
LEFT JOIN mu05iq_wc_product_meta_lookup parent_wc_product_meta_lookup
ON posts.post_type = 'product_variation' AND parent_wc_product_meta_lookup.product_id = posts.post_parent
WHERE posts.post_type IN ('product','product_variation')
AND ( ( ( posts.post_title LIKE '%fiber%') OR ( posts.post_excerpt LIKE '%fiber%') OR ( posts.post_content LIKE '%fiber%' ) OR ( wc_product_meta_lookup.sku LIKE '%fiber%' ) OR ( wc_product_meta_lookup.sku = "" AND parent_wc_product_meta_lookup.sku LIKE '%fiber%' ) ))
ORDER BY posts.post_parent ASC, posts.post_title ASC
WordPress database error: [Unknown column '1' in 'where clause']
SELECT COUNT( 1 ) FROM mu05iq_postmeta WHERE post_id IN( SELECT ID FROM mu05iq_posts WHERE post_type = 'product' ) && meta_value = "1" AND meta_key = '_yoast_wpseo_is_cornerstone'
If anyone has any ideas i would appreciate it.
Thanks and regards.

If someone is experiencing this issue, it's a digital ocean managed database setting, when you create a database cluster it sets up some settings in Global SQL Mode, just remove ANSI_QUOTES. That fixed my issue.

Related

Wordpress and author__in

I manipulate a query with pre_get_posts
But i want to show posts of multiple authors.
These for i use "author__in".
$query->set('author__in', [53, 285]);
But my query still runs with 2 post_author parameters. How can i exclude the second one?
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND wp_posts.ID IN (349956,327745,177707)
AND wp_posts.post_author IN (53,285)
AND (wp_posts.post_author = 53)
AND wp_posts.post_type = 'post'
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10
There are various author filters available: https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
Adding a new one won't remove others - so you'll need to remove the original author filter. You can do this using the following code:
unset( $query->query_vars['author'] );

wordpress exclude posts from loop if only has one specific category

I have this category which is "community-posts" I don't want it to appear on my homepage loop so I added this to my query
<?php query_posts(array('showposts' => 4,'category__not_in' => $id_communityposts,));?>
This is working fine with me but some "community-posts" I want them to be featured on the homepage loop. (exception)
so I want to only exclude the posts that has one category as "community-posts" if it has this category and more its shows normally.
First thing do not use query_posts - it should never be used as it alter the main query. Use get_posts instead - it's much safer and perform the same task.
To answer your question, let's first imagine how the query would look in SQL (assuming your $id_communityposts is equal to 2) :
SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_postmeta
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE
wp_posts.ID = wp_postmeta.post_id AND
(
(wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id NOT IN(2))
OR
(wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value = 1)
)
ORDER BY wp_posts.post_date DESC
LIMIT 4
So we query the post, post meta and taxonomy tables and make two possible conditions:
The category ID is not 2, OR
The featured meta key of the post is set to 1 (change this to whatever key / value depending of how you store the "featured" information).
For that kind of specific cases, get_posts isn't really good to play with - querying the DB with WPDB will give you much more flexibility.
$posts = $wpdb->get_results(
"SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.ID = $wpdb->postmeta.post_id AND
(
($wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN(2))
OR
($wpdb->postmeta.meta_key = 'featured' AND $wpdb->postmeta.meta_value = 1)
)
ORDER BY $wpdb->posts.post_date DESC
LIMIT 4"
);
Let me know if you run into any issue as it is an untested query.
If I understood the question correctly , The simplest solution, not involving complicated SQL would be something along the lines of :
// NOT TESTED !
if ( count(get_the_category()) > 1 ) { // this means there are more than single category
// show the desired posts
} else {
// dont show
}
read get_the_category() in codex
Along the same logic lines you could also use wp_get_post_categories

WordPress: How can add posts without category into category

I have some posts in my WordPress without category, Its above 2000 article, So i need to add this article into category that name is " News " by SQL
This should work:
UPDATE wp_term_relationships SET term_taxonomy_id = '3' WHERE term_taxonomy_id = '1';
The 3 is the id of the category "News", you might need to change it if it differs for you. The 1 is the category id for "Uncategorised", so the above should change all the post ids of 1 (Uncategorised) to 3 (News)
EDIT try:
UPDATE wp_term_relationships SET term_taxonomy_id = '3' WHERE term_taxonomy_id = '';
I didn't find the answer to this question, so maybe it will be helpful for someone.
you can try this code, but instead 777 - add your category id
INSERT IGNORE INTO wp_term_relationships
(object_id, term_taxonomy_id, term_order)
SELECT
wp_posts.ID as object_id,
777 as term_taxonomy_id,
0 as term_order
FROM wp_posts
LEFT JOIN
wp_term_relationships rel
ON wp_posts.ID = rel.object_id
LEFT JOIN
wp_term_taxonomy tax
ON tax.term_taxonomy_id = rel.term_taxonomy_id
AND tax.taxonomy = 'category'
LEFT JOIN
wp_terms term
ON term.term_id = tax.term_id
WHERE wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'
AND term.term_id is null
UPDATE FROM `wp_posts` WHERE `post_category` SET `post_category` = '9' WHERE `post_category` = '';

How to query WordPress Posts order by rating based on another table that contents the ratings for each post ID

I have a table that saves the ratings for each post, the table looks like this http://d.pr/YIxL it saves the ID of the post and the rating, I need to query the posts based on that table ASC or DSC.
I hope that the answer is clear.
This was the query that did the trick
$wp_query->request = "
SELECT $wpdb->posts.* FROM $wpdb->posts
Left join $ratings_table
ON $wpdb->posts.ID = $ratings_table.ID
Where
$wpdb->posts.post_type = 'games' AND $wpdb->posts.post_status = 'publish'
ORDER BY $ratings_table.points DESC
LIMIT $ppp OFFSET $offset";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);

Mass delete unpopular Tags

Mass delete unpopular Tags
I have 1000's of tags and I would like to simply delete all tags that aren't used more than X times... i.e. 5 times.
Does anyone know of a simple way to do this? Even straight SQL would totally ROCK!
Doing with with an SQL command is going to be your best bet. The tables wp_terms, wp_term_relationships, and wp_term_taxonomy are the ones of interest. WordPress keeps track of relationships with wp_term_taxonomy.count. So this help make life easier.
-- remove terms
DELETE FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;)
-- remove all relationships
DELETE FROM wp_term_relationships WHERE term_taxonomy_id IN (SELECT term_taxonomy_id FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;)
-- remove taxonomy entry
DELETE FROM wp_term_taxonomy WHERE taxonomy = 'tag' AND count <= 5;
Note: I would strongly suggest making a backup of these tables before running the commands above.
Make sure to back up the database first
DELETE a,c FROM wp_terms AS a
LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id
LEFT JOIN wp_term_relationships AS b on b.term_taxonomy_id = c.term_taxonomy_id
WHERE ( c.taxonomy = 'post_tag' AND c.count <= 5 );
On the last line, you could also modify c.count <= 5 and replace 5 with any number of your choice.
Jasons answer worked except in my install, the name of the taxonomy was 'post_tag' not 'tag'
$x = 5; // set this to any number
$sql = "SELECT `name` FROM `wp_terms`";
$result = mysql_query($sql);
$count = array();
while($row = mysql_fetch_assoc($result))
{
$count[$name]++;
}
foreach($count as $key = $value)
{
if($value < $x)
{
$sql2 = "DELETE FROM `wp_terms` WHERE `name` = '". $key ."'";
$result2 = mysql_query($sql2);
}
}
There's more efficient ways of doing it but this will do the job.
Edit: make a backup first. I'm not entirely sure that that table is exclusive to tags.

Resources