• Resolved pax1

    (@pax1)


    Hi there,

    I use this query to allow access to specific post categories:
    $query->set( ‘cat’, ‘1’);
    But when doing so, not only my posts without cat 1 are invisible, a lot of different elements are gone too (pages in the backend, media…).
    I necessarily need to allow cats. To use ( ‘cat’, ‘-x’) wont be an option right now..
    Does someone has an idea to fix this?

    Thanks in advance
    Pascal

    • This topic was modified 5 years, 10 months ago by pax1.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I assume you are doing this in a “pre_get_posts” action callback? You need some conditionals to only set your category for the appropriate requests. A common conditional is if ( ! is_admin() && $query->is_main_query()) :

    You may wish to also add one of the is_*() functions like is_front_page() or is_archive(), etc. It depends on where you really want to restrict the posts listed.

    BTW, when passing ID arguments, don’t place ID numbers in quotes. This is the way it should be: $query->set('cat', 1 );
    Your way works because PHP is loosely typed and the parser makes the string to integer conversion for you. But it’s better to do it correctly to start with ??

    Thread Starter pax1

    (@pax1)

    Hey bcworkz,

    thanks for the reply. I’ll try it with a different conditional ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Set allowed categories’ is closed to new replies.