2

I want to check to see if the current user has already submitted a post in our custom post type and if they have, and they have the author role then add inline style to the header to hide some things..

Here is what Im trying but it isn't working and I can't seem to figure out why?

function chelsea_user_has_posts($user_id) {
  $result = new WP_Query(array(
    'author'=>$user_id,
    'post_type'=>'ait-dir-item',
    'post_status'=>'publish',
    'posts_per_page'=>1,
  ));
  return (count($result->posts)!=0);
}

function cross_check_user_items() {
  $user = wp_get_current_user();
  $chelseaRole = get_the_author_meta('user_level');
  if ($user->ID)
    if (chelsea_user_has_posts($user->ID) && $chelseaRole == 1) {
      echo '<style type="text/css">
               #adminmenuwrap ul#adminmenu li#menu-posts-ait-dir-item.wp-has-submenu ul.wp-submenu li:nth-child(2), body.post-type-ait-dir-item div.wrap h2 a.add-new-h2, #woocommerce-product-data.postbox div.inside div.panel-wrap ul.product_data_tabs li:nth-child(8) {display:none !important;}
             </style>';
  } else {
    //show them the goods
  }
}
add_action('admin_head', 'cross_check_user_items');

All of this of course is in my functions.php file

Thanks for looking :-)

  • As it's already an instance of WP_Query, have you tried $result->have_posts()? – kaiser Mar 31 '14 at 23:02
2

Instead of using $chelseaRole ==1 it should be replaced with something like this !current_user_can('manage_categories') since the author role cannot manage categories as seen here: https://codex.wordpress.org/Roles_and_Capabilities

Polished it would look like this:

function chelsea_user_has_posts($user_id) {
  $result = new WP_Query(array(
    'author'=>$user_id,
    'post_type'=>'ait-dir-item',
    'post_status'=>'publish',
    'posts_per_page'=>1,
  ));
  return (count($result->posts)!=0);
}

function cross_check_user_items() {
  $user = wp_get_current_user();

    if (chelsea_user_has_posts($user->ID) && !current_user_can('manage_categories')) {
      echo '<style type="text/css">
               #adminmenuwrap ul#adminmenu li#menu-posts-ait-dir-item.wp-has-submenu ul.wp-submenu li:nth-child(3), body.post-type-ait-dir-item div.wrap h2 a.add-new-h2, #woocommerce-product-data.postbox div.inside div.panel-wrap ul.product_data_tabs li:nth-child(8) {display:none !important;}
             </style>';
  } else {
    //show them the goods
  }
}
add_action('admin_head', 'cross_check_user_items');
  • Still you should check against WP_Query->have_posts or count() the value of WP_Query->found_posts :) – kaiser Mar 31 '14 at 23:35
  • The role author can be changed. You can not rely on the default capabilities. – fuxia Apr 1 '14 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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