Site icon Hip-Hop Website Design and Development

match keywords to a category and assign a category to a wordpress post

I have my own rss feed import plugin. This plugin has a logic for matching keywords (separated by commas) with a category, according to the description.

There is a structure of categories and corresponding keywords:

category - keyword1, keyword2, keyword3
category2 - keyword2, keyword3, keyword4
category3 - keyword4, keyword5, keyword3

It works like this:

A set of keywords is taken, and each keyword is searched for in the feed description. And if any keyword is in the description, then the corresponding category is assigned to the post.

To search for words, I use the stripos() function;

The problem is that sometimes, posts are assigned a category, the description of which does not contain the corresponding categories. Those. sometimes categories are falsely assigned. And sometimes, the posts you want are not assigned at all.

I mean the problem is here:

$description = html_entity_decode( strip_tags($description_every), ENT_COMPAT, 'UTF-8');

At first glance, the code is correct. But I cannot understand why the incorrectness occurs when assigning categories. Can you please tell me where I am making a mistake here in the code?

Here’s the code itself:

function get_matched_categories( $description_every ) {
 $tables = get_field( 'table', 'option' );
 $description = html_entity_decode( strip_tags($description_every), ENT_COMPAT, 'UTF-8');
    if ( is_array( $tables ) ) {
        $arr_cat = array();
        foreach ( $tables as $child ) {
            foreach ( explode( ', ', $child['keywords'] ) as $keyword ) {
                if (stripos($description, $keyword) !== false) {
                    array_push( $arr_cat, $child['category'] );
                }
            }
        }
        return $arr_cat;
    }
}