0

Could anyone be kind enough to descrive how to I can detect (in my functions file) when admin an user is on the 'All Posts' page?

I have tried get_post_type() but it returns nothing.

Thanks.

3
  • What is All posts page ? A normal WordPress Page create through WP Admin -> Pages ?
    – Shazzad
    Commented Dec 30, 2013 at 22:57
  • The 'All Posts' page is shown below the 'Posts' page in the main admin sidebar. The URL for it on my setup is localhost/wptest/wp-admin/edit.php Commented Dec 30, 2013 at 23:23
  • Ok, anwser provided
    – Shazzad
    Commented Dec 31, 2013 at 0:04

3 Answers 3

2

If you need exactly on All Posts page, Not on All Pages, nor All Products or etc -

if 
(
    'edit.php' === $GLOBALS['pagenow']
    && ( 
        isset( $GLOBALS['typenow'] ) 
        && '' == $GLOBALS['typenow'] 
        || 'post' == $GLOBALS['typenow'] 
    )
)
{
    // Do what you like...
}
0

If I understood you correctly, this is what you are looking for:

if (
    'edit.php' === $GLOBALS['pagenow']
    && ! isset($_GET['action'])
) {
    // Do what you like...
}
1
  • Not quite. I need it to work when the same page is used to show the results from using the 'Search Posts' form on the same page - e.g., localhost/wptest/wp-admin/… Commented Dec 31, 2013 at 0:26
0

Take a look at get_current_screen():

function do_something_on_all_posts_page() {
    $screen = get_current_screen();
    if ( $screen['base'] == 'edit' && $screen['post_type'] == 'post' ) {
        /* Your code here */
    }
}
add_action( 'current_screen', 'do_something_on_all_posts_page' );
2
  • This answer doesn't check if the user is an admin - do you additionally need that criteria met in the answer, or were you only mentioning admin in reference to the page location (i.e. is_admin())? Commented Dec 31, 2013 at 3:05
  • Thanks. This looks like a much nicer solution, but I need to use this code with the 'posts_join', 'posts_where' and 'posts_groupby' hooks. And when I use the get_current_screen() function from those hooks, I get a white screen in the front end. Commented Jan 1, 2014 at 15:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.