Duplicate Queries
-
I am trying to develop wp theme, and I read that WP queries can slow down ur site, so I tried to fix this duplicate queries, I saw it when I used query monitor, but I have no idea, where’s the other wp query at all.
I add this code into functions.php
function add_custom_pt( $query ) { if ( !is_admin() && $query->is_main_query() ) { $query->set( 'post_type', array( 'videos' ) ); } } add_action( 'pre_get_posts', 'add_custom_pt' );It was supposed to show CPT in homepage instead of post, and I put loop in the index.php
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php endwhile; ?> <?php endif; ?>I only put this loop, and get the rest info from <?php the_permalink(); ?> <?php the_title(); ?> ,etc. But I have no idea why it count as 2 queries. If we used pre_get_posts doesn’t it supposed to modify the existing query only ? but it count as 2 queries. I don’t know, but maybe it’s normal ? I tried with twenty nineteen themes, it also count 2 duplicate queries.
-
I setup the same situation on my test site and I see that the CPT query only occurs once. Verified by error logging any CPT query, main or not. The Query Monitor plugin does list the same query twice, one under main query, then again under non-main core caller WP_Query->get_posts(). It’s the same query instance though, listed two different ways.
@bcworkz so that’s normal for default wordpress ?
Single post https://i.imgur.com/rr8Mmr5.jpg
Homepage https://i.imgur.com/NQ54Mit.jpgNo, the default behavior (at least according to my bog standard test site) is the one call is listed in two places in the monitor. But in each listing, no number of calls is listed since it only occurred once. I didn’t even know the monitor listed the call count! They all happen only once for me.
Perhaps you have a plugin active (besides query monitor) that’s causing redundant calls?
Well, no, I used a test site, whole make from scratch and install new wordpress with query monitor plugin only, and use default theme, all default theme front page got 2 duplicate queries. So I thought that was normal. I think will do more research about this if it’s not normal 🙁
I have seen a lot of tutorials online that make theme from scratch randomly used new wp_query, after I read that duplicate wp_query gonna cause website running slow, I become too paranoid about this.
FWIW, I do see two duplicate queries, but they are not the main query, and more importantly, one of the duplicates is due to query monitor itself. Duplicate is not normal in normal operation AFAICT.
Unless you have a very large DB, I’m not convinced extra queries will make a noticeable, real world impact. Just a hunch, I’ve no data to back this up. Query results are in some cases placed in an object cache, so repeated results come from memory and not the DB server.
Ok, thanks, that clear a lot of question.
Btw, can you teach me how u determine there’s only 1 query using error logging ?
I wanna know how u do the error logging.
Using the wp-debug only log any php error, not determining the query.I hooked the “pre_get_posts” action, which fires for any post query using
WP_Query::get_posts()class method. From the action callback I simply diderror_log('pre_get_posts had fired.');, then checked my error log after making a CPT query. There was only one entry.N.B: Some actions can fire more than once per request, so multiple log entries is not proof of multiple queries, but a single entry is proof of one query.
ETA: by “making a CPT query”, I mean I made a webpage request for a CPT archive listing. I didn’t directly call
WP_Query::get_posts()myself as that would obviously result in a single query.-
This reply was modified 4 years, 9 months ago by
bcworkz.
I don’t really get it.
Can I see the sample code ?
Does that error_log went into browser console or we need to enable wp-debug and it logs to wp-content ?In functions.php or a plugin:
add_action('pre_get_posts', function() { error_log('pre_get_posts had fired.'); });Request a CPT archive page, then check the error log. Results only mean something if there is no chance anyone else is using the site. I test things on a localhost site in my laptop.
The error log is where ever PHP is configured to put it. Some sites put it in the WP folders, which isn’t a good choice security-wise. Mine goes to /var/log/apache2/error.log. On hosted sites often you can get there through cPanel or similar. In some cases you can specify error handling behavior at run time. Ability to do so depends on server configuration.
The browser console is not a viable target because PHP errors happen server side, not client side. However, you could get errors to appear right on the page by defining WP_DEBUG as true, then calling
trigger_error('pre_get_posts had fired.');
in place of the error_log() call. This is not infallible because you may not see the message if the function executes when the browser is not expecting to receive output. Or a theme’s CSS could hide it, or it can occur in a strange unexpected position on the page.Okay, so I tried it, and here is some log that generated.
[Wed Aug 25 22:11:26.592698 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62911] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:26.592698 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62911] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:26.594699 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62911] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:26.633703 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62911] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:26.635703 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62911] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:26.639695 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62911] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:36.884743 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62266] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:36.884743 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62266] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:36.885745 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62266] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:36.925752 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62266] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:36.927761 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62266] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:36.934758 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62266] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:48.787980 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:49671] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:48.787980 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:49671] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:48.792975 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:49671] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:11:48.803989 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:49671] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:12:24.851753 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52878] pre_get_posts had fired., referer: http://localhost/wa/?s=logjfjjtj [Wed Aug 25 22:12:24.851753 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52878] pre_get_posts had fired., referer: http://localhost/wa/?s=logjfjjtj [Wed Aug 25 22:12:24.853755 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52878] pre_get_posts had fired., referer: http://localhost/wa/?s=logjfjjtj [Wed Aug 25 22:12:24.890762 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52878] pre_get_posts had fired., referer: http://localhost/wa/?s=logjfjjtj [Wed Aug 25 22:12:24.892764 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52878] pre_get_posts had fired., referer: http://localhost/wa/?s=logjfjjtj [Wed Aug 25 22:12:24.897765 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52878] pre_get_posts had fired., referer: http://localhost/wa/?s=logjfjjtj [Wed Aug 25 22:14:24.766346 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.766346 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.768949 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.814956 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.817957 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.818957 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.819957 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.820957 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:24.827961 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52062] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.188992 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.189992 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.193000 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.238016 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.240512 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.241512 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:14:59.246520 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:53733] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:15:32.543407 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52258] pre_get_posts had fired., referer: http://localhost/wa/?s=eum [Wed Aug 25 22:15:32.577397 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52258] pre_get_posts had fired., referer: http://localhost/wa/?s=eum [Wed Aug 25 22:15:32.579397 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52258] pre_get_posts had fired., referer: http://localhost/wa/?s=eum [Wed Aug 25 22:15:32.580397 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52258] pre_get_posts had fired., referer: http://localhost/wa/?s=eum [Wed Aug 25 22:15:32.582398 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:52258] pre_get_posts had fired., referer: http://localhost/wa/?s=eum [Wed Aug 25 22:15:32.925493 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:51780] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:32.934185 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:51780] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:32.936191 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:51780] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:32.937191 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:51780] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:32.939198 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:51780] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.320360 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.321360 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.322361 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.362362 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.364370 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.365370 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.366371 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.368364 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:15:51.372365 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:58747] pre_get_posts had fired., referer: http://localhost/wa/sapiente-inventore-harum-aliquam-similique-eum-voluptas/ [Wed Aug 25 22:16:04.593870 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.629865 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.631863 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.632864 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.633863 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.637865 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.640866 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.642868 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.644867 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.647867 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.649868 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.650868 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:04.652868 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:05.002050 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.039041 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.041050 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.043049 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.044057 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.048043 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.050051 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.051043 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.053044 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.056046 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.058045 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.059046 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:05.061047 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:61409] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.917359 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.917359 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.918359 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.956367 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.958369 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.959370 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.961370 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.962369 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:40.967372 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:62441] pre_get_posts had fired., referer: http://localhost/wa/magnam-amet-dolore-sit-esse/ [Wed Aug 25 22:16:51.514411 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:51651] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:51.548330 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:51651] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:51.551332 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:51651] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:51.552331 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:51651] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:51.554337 2021] [php7:notice] [pid 14000:tid 1836] [client ::1:51651] pre_get_posts had fired., referer: http://localhost/wa/ [Wed Aug 25 22:16:51.895407 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:60594] pre_get_posts had fired., referer: http://localhost/wa/voluptatum-aut-quia-vero-impedit-officia-fugiat/ [Wed Aug 25 22:16:51.902410 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:60594] pre_get_posts had fired., referer: http://localhost/wa/voluptatum-aut-quia-vero-impedit-officia-fugiat/ [Wed Aug 25 22:16:51.904417 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:60594] pre_get_posts had fired., referer: http://localhost/wa/voluptatum-aut-quia-vero-impedit-officia-fugiat/ [Wed Aug 25 22:16:51.906411 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:60594] pre_get_posts had fired., referer: http://localhost/wa/voluptatum-aut-quia-vero-impedit-officia-fugiat/ [Wed Aug 25 22:16:51.908419 2021] [php7:notice] [pid 14000:tid 1828] [client ::1:60594] pre_get_posts had fired., referer: http://localhost/wa/voluptatum-aut-quia-vero-impedit-officia-fugiat/Right now I am not using cpt query, and I only add filter search to post with pre_get_posts so it doesn’t include pages. How to check for duplicate queries in the log ?
Erm… I think I over simplified in an effort to offer an example, sorry. You need to condition the error log call to filter out other post queries done for the same request, like for menu items, etc. Something like
add_action('pre_get_posts', function( $query ) { if ($query->is_main_query()) error_log('pre_get_posts had fired for main query.'); });An entry should appear once for any kind of post request, a single post, a date archive, CPT, what ever.
Ok sir, it works great, now I can confirm all my main query only 1, not duplicated, I would guess because query monitor was activated, it count as 2 before.
After I use php error_log all the referrer only once. Thanks for the help, I shall mark it as solved.You’re welcome. One thing though, there is a situation where there could be inefficient, redundant queries worthy of correction that my suggested code wouldn’t catch. If someone were to code their template to make a new query that’s similar to the main query (or a replacement for), the new redundant query is not “main” and is thus ignored by our code. Twenty-nineteen theme wouldn’t do this, but custom coded or child themes might.
Coders new to WP will often do this because they don’t like the default main query, not realizing they can alter the main query through “pre_get_posts” action.
-
This reply was modified 4 years, 9 months ago by
The topic ‘Duplicate Queries’ is closed to new replies.