Wordpress:在单个自定义帖子类型页面上提交帖子方法表单返回404


Wordpress: Submitting a post method form on single custom post type page returns 404

我现在正在开发Wordpress主题。

我有一个名为activity:的自定义帖子类型

register_post_type($class::$post_type, array(
    'label' => $class::$post_type_name,
    'description' => $class::$post_type_description,
// ....

我正在编辑single-activity.php模板,上面有一个表单:

<form id="form_main" method="post" action="">
    <?php wp_nonce_field(); ?>
    <!-- Other fields -->
</form>

当我打开url http://localhost/activity/slug/时,页面运行良好。

但当我提交表单时,即使用post方法请求url时,它会导致404响应,url只是简单地相同。

怎么了?

经过长时间的调试,我发现了原因:$_POST字段影响了查询。

在我的案例中,我提交了一个包含关键字和值的字段:

$_POST['title'] = 'new title';

我打算用这个标题创建一个新的帖子。

但事实上,默认查询使用该字段作为where条件来过滤当前页面,最终没有结果。

因此,在发布数据时,注意保留字段名:

类似:

  • s
  • p
  • 所有权
  • 第页

有关详细信息,请查看文档。