简体   繁体   English

Wordpress函数在bash中运行的php脚本中不起作用

[英]Wordpress functions don't work in php script ran in bash

I have a PHP script (that is currently a WP page), that uses WP query to loop through a custom post type, updating some values as well as uploading some images to the media library. 我有一个PHP脚本(当前是WP页面),该脚本使用WP查询来遍历自定义帖子类型,更新一些值以及将一些图像上传到媒体库。 This script works fine as a page, but since it's both quite heavy and has to be run as a cronjob, I decided to adapt it to run in the cli, but it just refuses to work. 该脚本可以很好地作为页面使用,但是由于它既很沉重又必须作为cronjob运行,所以我决定对其进行修改以使其在cli中运行,但是它只是拒绝工作。

Currently, I'm requiring the wp-load.file before my script to get WP core, as well as parsing the args, putting them into the $_REQUEST array. 当前,我需要脚本之前的wp-load.file来获取WP内核,以及解析args,并将它们放入$_REQUEST数组中。 The script does run but doesn't return any output (except errors if there are any), or do the actual work so I can't debug it that way. 该脚本可以运行,但是不返回任何输出(除非有错误,否则不输出),或者不执行实际工作,因此我无法以这种方式对其进行调试。

What could be the problem and Why does it not work? 可能是什么问题,为什么不起作用?

(I'm running PHP 7.2 on WHM centos with WordPress 4.9.8) (我在WHM centos和WordPress 4.9.8上运行PHP 7.2)

edit: I have been able to debug the script using the error_log to the point that it does run correctly and returns output when appropriate(had to populate some members of the $_SERVER array needed for wp initialization), however the wordpress loop is empty when querying for a simple custom post type. 编辑:我已经能够使用error_log调试脚本,使其可以正确运行并在适当的时候返回输出(必须填充wp初始化所需的$ _SERVER数组的某些成员),但是wordpress循环为空查询简单的自定义帖子类型。 What could be the issue here? 这里可能是什么问题?

You need to include wp-load.php file at the top of the page. 您需要在页面顶部包含wp-load.php文件。

require_once("wp-load.php");

Or 要么

require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM