0

Excuse me if something like this has been asked before. So, I'm trying to alter the query which displays the posts of a custom post type, but on the admin side, more precisely, the edit.php page. This is my code:

function my_function() {
    global $pagenow, $typenow;
    if( 'edit.php' == $pagenow && 'certain-post-type' === $typenow ) {
      // do something
    }
}
add_action( 'admin_init', 'my_function' );

The main problem is not altering the query itself (yet!), but targeting only the edit.php page which lists the custom-post-type posts. It works when I hook onto the admin_init hook, but I understand that admin_init runs on every admin page load, which seems a bit unnecessary. I tried limiting this to fire only on the edit.php page for the custom post type by setting the hook to add_action( 'load-edit.php', 'my_function' );, but this did nothing.

Couldn't find much documentation on the load-{$pagenow} hook, hopefully the community can steer me in the right direction and tell me what I did wrong?

3
  • what is it that you're doing? There is a way to modify the main query for custom post type screens but it has nothing to do with admin_init, and you've left out what you were doing in your question and replaced it with // do something, can you restore the hidden code in your question and expand on the context? As well as why you need to do this and what the modification is? The answer to this question if taken literally isn't going to be very useful to you if your goal is to modify the query
    – Tom J Nowell
    Commented May 23, 2022 at 14:33
  • So, what I want to do is restrict access to custom post types in wp-admin based on user role AND taxonomy term. So let's say my taxonomy has two terms, 'term1' and 'term2', and I have a user role called "Specific Role". When I add a new user with the "Specific role" user role, I also want him to be able to access (and edit) only CPTs with the term 'term1'. If I create another user with the Specific Role user role, I want him to be able to edit only CPTs with the term 'term2' and so on. I guess I could hook to pre_get_posts but other than that I'm pretty much stuck. :/
    – Tatex
    Commented May 25, 2022 at 15:42
  • pre_get_posts is the answer I was going to write but then I re-read your question further and got confused because you're doing other things and haven't shared them. Can you use the edit link to re-add the code you mentioned but did not include? Note that not everybody reads comments so they'll have the same questions and confusions I have if not others. Also how is the role mapped to the term? How do we know that for "Specific role" we need to limit to term1 and not term2? What are you trying to implement that requires this?
    – Tom J Nowell
    Commented May 25, 2022 at 16:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.