Поддержка Проблемы и решения is_singular в хуке pre_get_posts

  • День добрый, функция is_singular(‘тип’) ничего не возвращает в хуке pre_get_posts

    хотя если выводим так $query->is_singular(); то выводит true, но хотелось бы знать причину почему так работает что с type false

    может кто сталкивался?

Просмотр 1 ответа (всего 1)
  • Потому что условные теги (функции) не работают до события posts_selection. В кодексе вот что пишут:

    Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

    However: if you have a reference to the query object (for example, from within the parse_query or pre_get_posts hooks), you can use the WP_Query conditional methods (eg: $query->is_search())

    https://codex.wordpress.org/Conditional_Tags

    Т. е. первое событие после которого можно использовать подобные функции это wp.

Просмотр 1 ответа (всего 1)
  • Тема «is_singular в хуке pre_get_posts» закрыта для новых ответов.