Wordpress and author__in - wordpress

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'] );

Related

Optimizing a wordpress + woocomm store with 100k+ products

So i'm in the middle of an issue right now where the page loading speed is not up to par to what we're looking for and i'm honestly out of ideas for now and i hoped maybe someone smarter can lead me in the right direction.
First of all, the environment:
The site is hosted on a cloud nginx server with 32GB of RAM.
The site uses a Porto theme with WP Bakery builder. I know i know, page builders aren't famous for speed but testing the same site with storefront and without bakery shows no difference.
The site is multilingual and for translation we use WPML. We've thought about using seperate language files instead but it is quite an hassle to get those strings out of wpml.
Now the situation:
Without any caching plugins / optimizers we get a 3-5s load speed. With WP Rocket + Redis we get the load speed less than a second. Now when the user visits a page and it is isnt yet cached by Redis (recently flushed) we'd like to prevent the user from waiting 3-5s to initally load the pages.
What i've tried (without caching):
Using QM to monitor and find plugins/scripts affecting the
performance.
Database indexing and cleanup tips found online.
Image optimization.
Query monitor shows me that on the shop page ( slowest ), wp posts main query takes more than 1 second to complete then adding additional 2-4s to display the data.
Using Scalability PRO plugin i tried limiting the time it takes to execute the query but honestly it didn't help much. None of the sql changes really helped alot nor did any other settings inside the plugin. Maybe a 1s difference.
Only real difference is seen when wp rocket and redis are active but thats not an ideal solution. Is there a way to make the site run any faster or to make it seem like it does?
Here's all the data i can get for /shop page
https://prnt.sc/be5qfPkzGim0
https://prnt.sc/lSmmvrEZR2X0
https://prnt.sc/1QasUlurvFgI
https://prnt.sc/TtYXI0MXaY5k
https://prnt.sc/dOib1Z1uGD4u
https://prnt.sc/vydXlQfq8x9W
Query
Caller: WP_Query->get_posts()
Rows: 12
Time (s): 1.4356
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
JOIN wp_icl_translations wpml_translations ON wp_posts.ID = wpml_translations.element_id
AND wpml_translations.element_type = CONCAT('post_', wp_posts.post_type)
WHERE 1=1
AND (wp_posts.ID NOT IN
(SELECT object_id
FROM wp_term_relationships
WHERE term_taxonomy_id IN (7,
9) ))
AND ((wp_postmeta.meta_key = '_price'
AND wp_postmeta.meta_value > '0'))
AND ((wp_posts.post_type = 'product'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')))
AND (((wpml_translations.language_code = 'en'
OR (wpml_translations.language_code = 'en'
AND wp_posts.post_type IN ('post',
'page',
'custom_css',
'customize_changeset',
'oembed_cache',
'user_request',
'product',
'product_variation',
'shop_order_refund')
AND ((
(SELECT COUNT(element_id)
FROM wp_icl_translations
WHERE trid = wpml_translations.trid
AND language_code = 'en' ) = 0)
OR (
(SELECT COUNT(element_id)
FROM wp_icl_translations t2
JOIN wp_posts p ON p.id = t2.element_id
WHERE t2.trid = wpml_translations.trid
AND t2.language_code = 'en'
AND (p.post_status = 'publish'
OR p.post_type='attachment'
AND p.post_status = 'inherit') ) = 0))))
AND wp_posts.post_type IN ('post',
'page',
'attachment',
'custom_css',
'customize_changeset',
'oembed_cache',
'user_request',
'wp_block',
'wp_template',
'wp_template_part',
'wp_navigation',
'product',
'product_variation',
'shop_order_refund',
'faq',
'porto_builder'))
OR wp_posts.post_type NOT IN ('post',
'page',
'attachment',
'custom_css',
'customize_changeset',
'oembed_cache',
'user_request',
'wp_block',
'wp_template',
'wp_template_part',
'wp_navigation',
'product',
'product_variation',
'shop_order_refund',
'faq',
'porto_builder'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.menu_order ASC,
wp_posts.post_title ASC
LIMIT 0,
12
Second slow query
Caller: _pad_term_counts()
Rows: 247746
Time (s): 1.0853
SELECT object_id, term_taxonomy_id
FROM wp_term_relationships
INNER JOIN wp_posts
ON object_id = ID
WHERE term_taxonomy_id IN (180862,181291,181328,181362,181541,182258,182825,180902,181307,181341,181373,181552,182698,181281,181318,181352,181384,181563,182752,180863,181292,181329,181363,181542,182259,182826,180927,181308,181342,181374,181553,182699,180853,181282,181319,181353,181385,181564,182753,180864,181293,181330,181364,181543,182260,182829,180928,181309,181343,181375,181554,182700,180854,181283,181320,181354,181386,181565,182754,180865,181294,181331,181365,181544,182261,183389,180929,181310,181344,181376,181555,182701,180855,181284,181321,181355,181387,181566,182778,180866,181295,181332,181366,181545,182645,183390,180930,181311,181345,181377,181556,182743,180856,181285,181322,181356,181388,181784,182779,180867,181301,181335,181367,181546,182646,183637,181243,181312,181346,181378,181557,182746,180857,181286,181323,181357,181389,181807,182780,180868,181302,181336,181368,181547,182647,184049,181275,181313,181347,181379,181558,182747,180858,181287,181324,181358,181390,182190,182781,180872,181303,181337,181369,181548,182648,208562,181276,181314,181348,181380,181559,182748,180859,181288,181325,181359,181391,182208,182782,180889,181304,181338,181370,181549,182649,208563,181277,181315,181349,181381,181560,182749,180860,181289,181326,181360,181539,182248,182783,180890,181305,181339,181371,181550,182696,181278,181316,181350,181382,181561,182750,180861,181290,181327,181361,181540,182249,182784,180896,181306,181340,181372,181551,182697,181279,181317,181351,181383,181562,182751)
AND post_type IN ('product')
AND post_status = 'publish'

Slow Query on Wordpress Search

Please I have a very slow query on my wordpress search, checking the processes it takes upto 40sec to execute using SHOW FULL PROCESSLIST; below is a sample of query
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%Industreet%') OR (wp_posts.post_excerpt LIKE '%Industreet%') OR (wp_posts.post_content LIKE '%Industreet%'))) AND (wp_posts.post_password = '') AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish') ORDER BY wp_posts.post_title LIKE '%Industreet%' DESC, wp_posts.post_date DESC LIMIT 0, 25
I have tried my best to fix this, but nothing is working out. though I have a total post of 120k on my site. I really need help.
Thanks in advance

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` = '';

wordpress generating slow mysql queries - is it index problem?

I've got very slow Mysql queries coming up from my wordpress site. It's making everything slow and I think this is eating up CPU usage. I've pasted the Explain results for the two most frequently problematic queries below. This is a typical result - although very occasionally teh queries do seem to be performed at a more normal speed.
I have the usual wordpress indexes on the database tables. You will see that one of the queries is generated from wordpress core code, and not from anything specific - like the theme - for my site.
I have a vague feeling that the database is not always using the indexes/is not using them properly...
Is this right? Does anyone know how to fix it? Or is it a different problem entirely?
Many thanks in advance for any help anyone can offer - it is hugely appreciated
Query: [wp-blog-header.php(14): wp()]
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 6
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
SIMPLE
wp_posts
ref
type_status_date
type_status_date
63
const
427
Using where; Using filesort
Query time: 34.2829 (ms)
9) Query: [wp-content/themes/LMHR/index.php(40): query_posts()]
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.ID NOT IN ( SELECT tr.object_id FROM wp_term_relationships AS tr INNER JOIN wp_term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ('217', '218', '223', '224') ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 6
id
select_type
table
type
possible_keys
key
key_len
ref
rows
Extra
1
PRIMARY
wp_posts
ref
type_status_date
type_status_date
63
const
427
Using where; Using filesort
2
DEPENDENT SUBQUERY
tr
ref
PRIMARY,term_taxonomy_id
PRIMARY
8
func
1
Using index
2
DEPENDENT SUBQUERY
tt
eq_ref
PRIMARY,term_id_taxonomy,taxonomy
PRIMARY
8
antin1_lovemusic2010.tr.term_taxonomy_id
1
Using where
Query time: 70.3900 (ms)
Check this http://core.trac.wordpress.org/ticket/10964
The problem is SQL_CALC_NUM_ROWS, WordPress set this param automatically when you execute get_posts query, this make a slow query.
You can try wp-cache plugin.
Also you can read this article it explains that SQL_CALC_FOUND_ROWS not the best solution at queries.

Resources