$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>get_queried_object_id in AJAX|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

get_queried_object_id in AJAX

matteradmin8PV0评论

Can anyone please tell me how to get the get_queried_object_id from the AJAX file?

What I've tried is:

$object_id = get_queried_object_id();
print_r($object_id); die();
// Outputs 0

global $wp_query;
$object_id = $wp_query->get_queried_object_id();
print_r($object_id); die('');
// Outputs 0

As far as I can tell, I won't be able to get the get_queried_object_id within the AJAX, so how am I supposed to get it in the right way, please?

Update: the output for the global $wp_query;var_dump($wp_query);exit;

["queried_object"]=>
  NULL
  ["queried_object_id"]=>
  NULL
  ["request"]=>
  NULL
  ["posts"]=>
  NULL
  ["post_count"]=>
  int(0)
  ["current_post"]=>
  int(-1)
  ["in_the_loop"]=>
  bool(false)
  ["post"]=>
  NULL
  ["comments"]=>
  NULL
  ["comment_count"]=>
  int(0)
  ["current_comment"]=>
  int(-1)
  ["comment"]=>
  NULL
  ["found_posts"]=>
  int(0)
  ["max_num_pages"]=>
  int(0)
  ["max_num_comment_pages"]=>
  int(0)
  ["is_single"]=>
  bool(false)
  ["is_preview"]=>
  bool(false)
  ["is_page"]=>
  bool(false)
  ["is_archive"]=>
  bool(false)
  ["is_date"]=>
  bool(false)
  ["is_year"]=>
  bool(false)
  ["is_month"]=>
  bool(false)
  ["is_day"]=>
  bool(false)
  ["is_time"]=>
  bool(false)
  ["is_author"]=>
  bool(false)
  ["is_category"]=>
  bool(false)
  ["is_tag"]=>
  bool(false)
  ["is_tax"]=>
  bool(false)
  ["is_search"]=>
  bool(false)
  ["is_feed"]=>
  bool(false)
  ["is_comment_feed"]=>
  bool(false)
  ["is_trackback"]=>
  bool(false)
  ["is_home"]=>
  bool(false)
  ["is_404"]=>
  bool(false)
  ["is_embed"]=>
  bool(false)
  ["is_paged"]=>
  bool(false)
  ["is_admin"]=>
  bool(false)
  ["is_attachment"]=>
  bool(false)
  ["is_singular"]=>
  bool(false)
  ["is_robots"]=>
  bool(false)
  ["is_posts_page"]=>
  bool(false)
  ["is_post_type_archive"]=>
  bool(false)
  ["query_vars_hash":"WP_Query":private]=>
  bool(false)
  ["query_vars_changed":"WP_Query":private]=>
  bool(true)
  ["thumbnails_cached"]=>
  bool(false)
  ["stopwords":"WP_Query":private]=>
  NULL
  ["compat_fields":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(15) "query_vars_hash"
    [1]=>
    string(18) "query_vars_changed"
  }
  ["compat_methods":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(16) "init_query_flags"
    [1]=>
    string(15) "parse_tax_query"
  }
}

Can anyone please tell me how to get the get_queried_object_id from the AJAX file?

What I've tried is:

$object_id = get_queried_object_id();
print_r($object_id); die();
// Outputs 0

global $wp_query;
$object_id = $wp_query->get_queried_object_id();
print_r($object_id); die('');
// Outputs 0

As far as I can tell, I won't be able to get the get_queried_object_id within the AJAX, so how am I supposed to get it in the right way, please?

Update: the output for the global $wp_query;var_dump($wp_query);exit;

["queried_object"]=>
  NULL
  ["queried_object_id"]=>
  NULL
  ["request"]=>
  NULL
  ["posts"]=>
  NULL
  ["post_count"]=>
  int(0)
  ["current_post"]=>
  int(-1)
  ["in_the_loop"]=>
  bool(false)
  ["post"]=>
  NULL
  ["comments"]=>
  NULL
  ["comment_count"]=>
  int(0)
  ["current_comment"]=>
  int(-1)
  ["comment"]=>
  NULL
  ["found_posts"]=>
  int(0)
  ["max_num_pages"]=>
  int(0)
  ["max_num_comment_pages"]=>
  int(0)
  ["is_single"]=>
  bool(false)
  ["is_preview"]=>
  bool(false)
  ["is_page"]=>
  bool(false)
  ["is_archive"]=>
  bool(false)
  ["is_date"]=>
  bool(false)
  ["is_year"]=>
  bool(false)
  ["is_month"]=>
  bool(false)
  ["is_day"]=>
  bool(false)
  ["is_time"]=>
  bool(false)
  ["is_author"]=>
  bool(false)
  ["is_category"]=>
  bool(false)
  ["is_tag"]=>
  bool(false)
  ["is_tax"]=>
  bool(false)
  ["is_search"]=>
  bool(false)
  ["is_feed"]=>
  bool(false)
  ["is_comment_feed"]=>
  bool(false)
  ["is_trackback"]=>
  bool(false)
  ["is_home"]=>
  bool(false)
  ["is_404"]=>
  bool(false)
  ["is_embed"]=>
  bool(false)
  ["is_paged"]=>
  bool(false)
  ["is_admin"]=>
  bool(false)
  ["is_attachment"]=>
  bool(false)
  ["is_singular"]=>
  bool(false)
  ["is_robots"]=>
  bool(false)
  ["is_posts_page"]=>
  bool(false)
  ["is_post_type_archive"]=>
  bool(false)
  ["query_vars_hash":"WP_Query":private]=>
  bool(false)
  ["query_vars_changed":"WP_Query":private]=>
  bool(true)
  ["thumbnails_cached"]=>
  bool(false)
  ["stopwords":"WP_Query":private]=>
  NULL
  ["compat_fields":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(15) "query_vars_hash"
    [1]=>
    string(18) "query_vars_changed"
  }
  ["compat_methods":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(16) "init_query_flags"
    [1]=>
    string(15) "parse_tax_query"
  }
}
Share Improve this question edited Feb 20, 2019 at 6:22 lezuduhizu asked Feb 20, 2019 at 6:08 lezuduhizulezuduhizu 134 bronze badges 4
  • You need to show more code. Where is the query coming from & what javascript are you using to access the ajax-endpoint? – admcfajn Commented Feb 20, 2019 at 6:13
  • @admcfajn I'm using jquery with post and json; ajax-endpoint isn't an issue; – lezuduhizu Commented Feb 20, 2019 at 6:15
  • So, if you global $wp_query;var_dump($wp_query);exit; you're good? You see the query? – admcfajn Commented Feb 20, 2019 at 6:16
  • @admcfajn, I'm posting it in the Post, as it says its too many characters – lezuduhizu Commented Feb 20, 2019 at 6:21
Add a comment  | 

1 Answer 1

Reset to default 0

OK, let's start from trying to understand, what get_queried_object function does. According to its Codex page:

Retrieve the currently-queried object. For example:

  • if you're on a single post, it will return the post object
  • if you're on a page, it will return the page object
  • if you're on an archive page, it will return the post type object
  • if you're on a category archive, it will return the category object
  • if you're on an author archive, it will return the author object etc.

As you can see, this function returns the object that should be queried for current request based on the URL of this request.

But AJAX calls are other, independent requests. If you use AJAX API, as you should, then you're creating a request to wp-admin/admin-ajax.php file. So there is no object queried for that request.

If you need the queried object in your AJAX function, then you have to pass it on in the request you make in JS.

Let's say, we're on single post page. The get_queried_object function returns that object. And we want to perform some action with AJAX and we need that post object inside that action. In such case you have to:

  1. Pass the ID of that queried object to JS script.
  2. Use that ID in JS and put it in data that is sent with your AJAX request.
  3. Use that data in your AJAX callback function.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far