• Resolved ahmed3baid

    (@ahmed3baid)


    Hi, I want to check if a post has specific category. I used the in_category() function. I passed two argument, the category name and post ID. The problem is when I pass the post ID as a number (e.g. in_category(‘uncategorized’, 876)) it works. But when I pass it as id (e.g. post->ID ) I don’t get anything. Any suggestions?

Viewing 15 replies - 1 through 15 (of 31 total)
  • Make sure you’re accessing the global $post variable first:

    global $post;
    if ( in_category( 'uncategorized', $post->ID ) ) :

    If you’re inside the loop (between while( have_posts() ) : the_post(); and endwhile; you can just use get_the_ID():

    in_category( 'uncategorized', get_the_ID() );

    Thread Starter ahmed3baid

    (@ahmed3baid)

    First thank you, but I’m not inside a loop, I’m getting the post object from other function.

    Can you share that function?

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Ok, I modified Ryan Fugate example on Adding Custom Notifications to buddypress. I wanted to add posts notifications and it worked fine, except when I tried to filter them. First you need to know how I want to filter them. – I’ve a profile field named ‘Field’ which is a dropdown list with 5 options. – I have also 5 post categories with the exact names of the ‘Field’ values. – When a post is added, If it has (e.g. A,B,C) categories I want a notification for every user who has his ‘Field’ value is equal to A,B or C.
    Here is the full code:

    function custom_filter_notifications_get_registered_components( $component_names = array() ) {
      if ( ! is_array( $component_names ) ) {
        $component_names = array();
      }
      array_push( $component_names, 'custom' );
      return $component_names;
    }
    add_filter( 'bp_notifications_get_registered_components', 'custom_filter_notifications_get_registered_components' );
    function custom_format_buddypress_notifications( $action, $item_id, $secondary_item_id, $total_items, $format = 'string' ) {
      if ( 'custom_action' === $action ) {
        $post = get_post( $item_id );
        $recent_author = get_user_by( 'ID', $post->post_author );
        $author_display_name = $recent_author->display_name;
        $custom_title = $author_display_name . ' has posted a new post: ' . get_the_title($post);
        $custom_text = $author_display_name. ' has posted a new post: ' . get_the_title($post);
        // WordPress Toolbar
        if ( 'string' === $format ) {
          $return = apply_filters( 'custom_filter', '<a href="' . esc_url( get_permalink( $post ) ) . '" title="' . esc_attr( $custom_title ) . '">' . esc_html( $custom_text ) . '</a>', $custom_text, $custom_link );
        // Deprecated BuddyBar
        } else {
          $return = apply_filters( 'custom_filter', array(
            'text' => $custom_text,
            'link' => $custom_link
          ), $custom_link, (int) $total_items, $custom_text, $custom_title );
        }
    
        return $return;
    
      }
    
    }
    
    add_filter( 'bp_notifications_get_notifications_for_user', 'custom_format_buddypress_notifications', 10, 5 );
    function bp_custom_add_notification( $post_id, $post_object ) {
    
      $args = array(
      'blog_id'      => $GLOBALS['blog_id'],
      'role'         => 'employer',
      'role__in'     => array(),
      'role__not_in' => array(),
      'meta_key'     => '',
      'meta_value'   => '',
      'meta_compare' => '',
      'meta_query'   => array(),
      'date_query'   => array(),
      'include'      => array(),
      'exclude'      => array(),
      'orderby'      => 'login',
      'order'        => 'ASC',
      'offset'       => '',
      'search'       => '',
      'number'       => '',
      'count_total'  => false,
      'fields'       => 'all',
      'who'          => ''
     );
      $all_users = get_users($args);
    
    foreach ($all_users as  $user) {
          $acc_type = xprofile_get_field_data('Field', $user->ID);
             if (in_category($acc_type, $post_object->ID)) {
                bp_notifications_add_notification( array(
              'user_id'           => $user->ID,
              'item_id'           => $post_id,
              'component_name'    => 'custom',
              'component_action'  => 'custom_action',
              'date_notified'     => bp_core_current_time(),
              'is_new'            => 1,
          ) );
             }
    
    }
    
    }
    add_action( 'wp_insert_post', 'bp_custom_add_notification', 99, 2 );

    PS: It works without the IF STATEMENT.

    Does it work if you change

    if (in_category($acc_type, $p_id)) {

    to

    if (in_category($acc_type, $post_id)) {

    ?

    Thread Starter ahmed3baid

    (@ahmed3baid)

    No, it doesn’t.

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Can I make the $post object global and access it directly? Because I get the Id in custom_format_buddypress_notifications() Function.

    I’m not sure what the global $post variable would be at that point, but your hook apparently passes in the the post object, so $post_object->ID should work. But if that worked $post_id should also work.

    Moderator keesiemeijer

    (@keesiemeijer)

    Probably not relevant, but try casting it to an integer:

    if (in_category($acc_type, (int) $post_object->ID)) {
    if (in_category($acc_type, (int) $post_id)) {

    Are you sure $acc_type always returns a category ID, name or slug?

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Thanks keesiemeijer, but I already checked this (the casting). $acc_type returns a string with a category name.

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Jacob Peattie, I cant get it work either way.

    Can you add this to your code and then share with us what the error log says:

    error_log( 'in_category(' . $acc_type . ', ' . $post_object->ID . ')' );

    Thread Starter ahmed3baid

    (@ahmed3baid)

    [12-Aug-2016 12:26:14 UTC] in_category(Research, 731)
    [12-Aug-2016 12:26:14 UTC] in_category(Services, 731)
    [12-Aug-2016 12:26:14 UTC] in_category(Technology and IT, 731)
    [12-Aug-2016 12:26:14 UTC] in_category(Administration, 731)
    [12-Aug-2016 12:26:14 UTC] in_category(Research, 731)

    Everything seems perfectly ok. The only issue I can imagine is if the category names don’t actually match those strings.

    What exactly isn’t working? The notification’s not appearing?

    Thread Starter ahmed3baid

    (@ahmed3baid)

    They don’t all match, I have more categories in the extra field. But I have many matching categories like Research and Services for example. The If must return TRUE when it examines those matching ones. When I passed a post id (number) it worked.

    Yeah I don’t get the notification.

Viewing 15 replies - 1 through 15 (of 31 total)
  • The topic ‘Check if a post has a specific Category’ is closed to new replies.