最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Store a value in global scope after init hook is fired

programmeradmin5浏览0评论

I am working now on a Settings page. As I will need data about post types, taxonomies, and posts repeatedly in my setting page, I preferred to get these variables in the global scope and call them in the setting fields to minimize the database queries.

This was working well for built-in post types and taxonomies(i.e posts, pages, categories, and tags). However, neither custom post types nor custom taxonomies data can be retrieved as they are registered when the init hook fires. Here is my code snippet:

<?php
//I need to get these variables after init hook is fired to get all cpt and custom taxonomies data

$current_taxonomies_names = get_taxonomies($args, 'names');
$current_post_type_names = get_post_types($args, 'names');
$query_posts = get_posts($query_args);

//The above variables shall be used repeatedly in the below function

function appearance_settings()
{

  //Adding section and fields for the settings page
  add_settings_section('structure', 'App Structure', 'structure', 'main');
  add_settings_field('post_types', __('Post Types', ''), 'post_types', 'main', 'structure');
  add_settings_field('taxonomies', __('Taxonomies', ''), 'taxonomies', 'main', 'structure');
  add_settings_field('excluded_posts', __('Excluded Posts', ''), 'excluded_posts', 'main', 'structure');
  add_settings_field('excluded_taxes', __('Excluded Taxonomies', ''), 'excluded_taxes', 'main', 'structure');
}

So is there a way to get $current_taxonomies_names, $current_post_type_names or $query_posts after the init hook is fired?

If there is no way, is there any alternative to get these variables without keeping on querying the database in every setting field callback function?

I am working now on a Settings page. As I will need data about post types, taxonomies, and posts repeatedly in my setting page, I preferred to get these variables in the global scope and call them in the setting fields to minimize the database queries.

This was working well for built-in post types and taxonomies(i.e posts, pages, categories, and tags). However, neither custom post types nor custom taxonomies data can be retrieved as they are registered when the init hook fires. Here is my code snippet:

<?php
//I need to get these variables after init hook is fired to get all cpt and custom taxonomies data

$current_taxonomies_names = get_taxonomies($args, 'names');
$current_post_type_names = get_post_types($args, 'names');
$query_posts = get_posts($query_args);

//The above variables shall be used repeatedly in the below function

function appearance_settings()
{

  //Adding section and fields for the settings page
  add_settings_section('structure', 'App Structure', 'structure', 'main');
  add_settings_field('post_types', __('Post Types', ''), 'post_types', 'main', 'structure');
  add_settings_field('taxonomies', __('Taxonomies', ''), 'taxonomies', 'main', 'structure');
  add_settings_field('excluded_posts', __('Excluded Posts', ''), 'excluded_posts', 'main', 'structure');
  add_settings_field('excluded_taxes', __('Excluded Taxonomies', ''), 'excluded_taxes', 'main', 'structure');
}

So is there a way to get $current_taxonomies_names, $current_post_type_names or $query_posts after the init hook is fired?

If there is no way, is there any alternative to get these variables without keeping on querying the database in every setting field callback function?

Share Improve this question edited Oct 4, 2020 at 13:19 user9559590 asked Oct 4, 2020 at 12:20 user9559590user9559590 11 bronze badge 5
  • Can you not move the get_taxonomies and get_post_types calls into those functions? You don't gain anything by making them shared/global variables – Tom J Nowell Commented Oct 4, 2020 at 14:09
  • For sure I can. But I have many functions and I don't want to make excessive database queries for each setting field especially that these variables are nearly all site public data, there is also a call to get_posts as well. Anyway, I found that the best approach is to use a class and set all these variables after the init hook is fired in its __construct() function. This solved the issue. – user9559590 Commented Oct 4, 2020 at 14:14
  • neither get_taxonomies or get_post_types access the database – Tom J Nowell Commented Oct 4, 2020 at 18:10
  • I checked and I found that you are right. For me, this is new information. Thank you. But anyway I still need the same for get_posts. – user9559590 Commented Oct 6, 2020 at 8:20
  • if you have a caching plugin or an object cache then it'll cache that on the same request too, the post objects at least will be cached so if there is a second query it will be much cheaper as it only needs to fetch the IDs – Tom J Nowell Commented Oct 6, 2020 at 8:35
Add a comment  | 

1 Answer 1

Reset to default 0

So after consulting on Facebook Advanced WordPress group, the advice was to create an object with setters and getters instead of using the declaring and getting variables in the global scope as shown in the code below:

class MyPlugin
{
  public $current_post_types;

  public function __construct()
  {
    add_filter('init', array($this, 'get_variables'));
  }


  public function get_variables()
  {
    $this->current_post_types = get_post_types(array(
      'public' => true,
      'show_in_rest'   => true,
    ), 'objects');
  }
}

$skeleton = new MyPlugin();

function mk_post_types()
{
  global $skeleton;
  $current_post_types = $skeleton->current_post_types;
  //remaining code below
}

This solved the issue, now I can get all custom post types in the mk_post_types function.

发布评论

评论列表(0)

  1. 暂无评论