0

I am going to make plugin, collecting post views from Google Analytics. The main idea is to get views per post and store them with post meta.

Problem:
My blog has a lot of posts and google analytics returns data very slow. It is about 30min for example to collect all data.

Possible solutions:

  1. Use Linux crontab to execute /cron.php file from my plugin folder. This file should contains something like: require_once('/../../../wp-blog-header.php');

  2. Use wp_schedule_event function to execute heavy query using wordpress core.

How do you think, what method more preferred and what props and cons.

0
  1. How many posts do you have? - Maybe there are some delays because of hosting?

  2. You want to create your own plugin collecting the data from Google Analytics - I'm not sure you're able to do that.

  3. "The main idea is to get views per post and store them with post meta." - Why do you want to collect that in post meta? If you want to collect this data - I recommend you to create your own tables in the database.

There is a lot of ready PHP scripts which you can use for creating your custom plugin on WP. It's the fastest way I guess.

A sample of my custom plugin for collecting stats (from the whole website).

enter image description here

  • 1
    Hi Greg. You didn't answer my question. – Anton Lukin Apr 10 '18 at 8:18
  • Hi Anton, I've two websites which are running queries (~15min execute) every 30min and it's working on wp_schedule_event function. I cannot compare it to Linux corntab - because I've not used that. WP Corn always works ok for me (this website is working almost 1year without any errors according to execute that queries). – Greg Much Apr 10 '18 at 8:34
  • 1
    To ask for additional information, please use comments, not answers. – cybmeta Apr 13 '18 at 6:15
0

1) You should forget using default wp-cron in such methods, when execution needs more than appx. 30 seconds.

using default WP-cron method (wp_schedule_event..) is bad because then your custom large query might start execution when user visits your website, making him to wait for many seconds and finally leaves the site (without waiting execution to be finished), and cron wont be finished ever, and that might cause other regular wp-scheduled crons not ever reach to execution. Leave default WP-CRON for simple things.

2) So you MUST use the server-side cron command (either in LINUX or whatever) in such cases (and obviously you'd use pure SQL queries, if you are updating many posts/post-metas) but I doubt that even that might help because I think server wont allow you to run the script for i.e. 30 minutes or so (unless you have unlimited permission to modify things as you wish).

3) The most important advise I always say: You are doing something wrong. Dont do wrong things, instead try to accommodate what there is already - there should be apis or some other ways to achieve what you want, without MANUAL trickchy sufferings.

  • Thanks for reply. Take a look at this answer, please: wordpress.stackexchange.com/a/126002/126253 My site works on own dedicated server and I can execute WordPress cron jobs manually, using system cron with define('DISABLE_WP_CRON', true); – Anton Lukin Apr 15 '18 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.