• 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 - 16 through 30 (of 31 total)
  • Thread Starter ahmed3baid

    (@ahmed3baid)

    When I added this line:
    error_log( in_category( $acc_type, $post_object->ID ));
    I got Empty log records:
    [12-Aug-2016 12:43:54 UTC]
    [12-Aug-2016 12:43:54 UTC]
    [12-Aug-2016 12:43:54 UTC]
    [12-Aug-2016 12:43:54 UTC]
    [12-Aug-2016 12:43:54 UTC]
    [12-Aug-2016 12:43:54 UTC]
    Shouldn’t I be getting True/False?

    Not sure how the log handles booleans. Could try doing this to get a true/false string:
    error_log( in_category( $acc_type, $post_object->ID ) ? 'true' : 'false' );

    Thread Starter ahmed3baid

    (@ahmed3baid)

    I got Falses
    [12-Aug-2016 13:11:05 UTC] false
    [12-Aug-2016 13:11:05 UTC] false
    [12-Aug-2016 13:11:05 UTC] false
    [12-Aug-2016 13:11:05 UTC] false
    [12-Aug-2016 13:11:05 UTC] false
    [12-Aug-2016 13:11:05 UTC] false

    Thread Starter ahmed3baid

    (@ahmed3baid)

    although I selected all the categories in this post

    Can you try logging:

    error_log( get_the_category( $post_object->ID ) );

    I’m wondering if wp_insert_post is too early to know if the post has any categories yet. You could also try changing:

    add_action( 'wp_insert_post', 'bp_custom_add_notification', 99, 2 );

    to

    add_action( 'save_post', 'bp_custom_add_notification', 99 );

    and change the function to

    function bp_custom_add_notification( $post_id ) {

    (and any reference to $post_object->ID to $post_id)

    Thread Starter ahmed3baid

    (@ahmed3baid)

    This is really makes sense, but I got blank log records when I tried to get the category.
    I did your steps but still no luck

    OOps, that might just be because you can’t log an array directly. Try:

    error_log( print_r( get_the_category( $post_object->ID ), true ) );

    Thread Starter ahmed3baid

    (@ahmed3baid)

    [12-Aug-2016 13:45:06 UTC] Array
    (
    )

    [12-Aug-2016 13:45:06 UTC] Array
    (
    )

    [12-Aug-2016 13:45:06 UTC] Array
    (
    )

    [12-Aug-2016 13:45:06 UTC] Array
    (
    )

    [12-Aug-2016 13:45:06 UTC] Array
    (
    )

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Sorry, I got this
    [12-Aug-2016 13:50:26 UTC] Array
    (
    [0] => WP_Term Object
    (
    [term_id] => 1
    [name] => Health
    [slug] => health
    [term_group] => 0
    [term_taxonomy_id] => 1
    [taxonomy] => category
    [description] =>
    [parent] => 0
    [count] => 27
    [filter] => raw
    [cat_ID] => 1
    [category_count] => 27
    [category_description] =>
    [cat_name] => Health
    [category_nicename] => health
    [category_parent] => 0
    )

    )

    Thread Starter ahmed3baid

    (@ahmed3baid)

    I got this record (Health category) even if the post doesn’t have Health category.

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Health is my default category

    Curious.

    So, just to confirm, the post being updated is a Post, right? Not a Custom Post Type? And the categories aren’t a custom taxonomy?

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Yeah, it’s regular Post and regular categories.

    Thread Starter ahmed3baid

    (@ahmed3baid)

    Any thoughts, Jacob?

    Thread Starter ahmed3baid

    (@ahmed3baid)

    It worked finally, I used set_object_terms hook. Apparently save_post and publish_post fire before the categories are set, because using them I can get Post title, post author,…

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