Foreach multiplying results coming from the database (PHP / Wordpress)

0

How do I stop foreach from multiplying results from the database?

I have the following function:

$categoriess = (array) get_terms( 'job_category', array('hide_empty'=>false) );

echo '<pre>'; print_r($categoriess); echo '</pre>';

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 53
            [name] => Administrador Banco de Dados - DBA
            [slug] => administrador-banco-de-dados-dba
            [term_group] => 0
            [term_taxonomy_id] => 53
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 0
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 54
            [name] => Administrador de Dados - AD
            [slug] => administrador-de-dados-ad
            [term_group] => 0
            [term_taxonomy_id] => 54
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 0
            [filter] => raw
        )

    [2] => WP_Term Object
        (
            [term_id] => 55
            [name] => Administrador de Redes
            [slug] => administrador-de-redes
            [term_group] => 0
            [term_taxonomy_id] => 55
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 0
            [filter] => raw
        )

    [3] => WP_Term Object
        (
            [term_id] => 56
            [name] => Administrador de Sistemas
            [slug] => administrador-de-sistemas
            [term_group] => 0
            [term_taxonomy_id] => 56
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 0
            [filter] => raw
        )

    [4] => WP_Term Object
        (
            [term_id] => 58
            [name] => Analista DBM
            [slug] => analista-dbm
            [term_group] => 0
            [term_taxonomy_id] => 58
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 0
            [filter] => raw
        )

    [5] => WP_Term Object
        (
            [term_id] => 59
            [name] => Analista de Aplicações
            [slug] => analista-de-aplicacoes
            [term_group] => 0
            [term_taxonomy_id] => 59
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    [6] => WP_Term Object
        (
            [term_id] => 60
            [name] => Analista de BI
            [slug] => analista-de-bi
            [term_group] => 0
            [term_taxonomy_id] => 60
            [taxonomy] => job_category
            [description] => 
            [parent] => 0
            [count] => 0
            [filter] => raw
        )
      ... e assim por diante trazendo todas as categorias cadastradas no banco de dados
)

And I have this SELECT that performs a query in the database bringing the values I want.

foreach ($categoriess as $key) {
        $query10 = $wpdb->get_results("
            SELECT * FROM $wpdb->usermeta LEFT JOIN $wpdb->users ON($wpdb->users.ID = $wpdb->usermeta.user_id) LEFT JOIN $wpdb->posts ON($wpdb->posts.post_author = $wpdb->users.ID)
            LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
            LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
            LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
            LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
            WHERE $wpdb->posts.post_type = 'noo_resume'
            AND $wpdb->posts.post_status = 'publish'
            AND $wpdb->postmeta.meta_key = '_job_category'
            AND $wpdb->postmeta.meta_value LIKE '%{$key->term_id}%'
            AND $wpdb->usermeta.meta_key = '_jm_candidate_field_clocknow_user_btn'
            AND $wpdb->usermeta.meta_value = 'value_1'
        ");

        foreach ($query10 as $vaga) {
                echo 'Nome: ' .$vaga->display_name. '<br>';
                echo 'Currículo: ' .$vaga->post_title. '<br>';
                echo 'Term ID: ' .$vaga->meta_value. '<br>';
                echo 'Cargo: ' .$key->name. '<br>';
                echo '<hr>';
        }
    }

The result of the query is as follows:

Name: Rodrigo Fontes Santos

Curriculum: Test

Term ID: ["55", "56", "60"]

Position: Network Administrator

Name: Rodrigo Fontes Santos

Curriculum: Test

Term ID: ["55", "56", "60"]

Position: Systems Administrator

Name: Rodrigo Fontes Santos

Curriculum: Test

Term ID: ["55", "56", "60"]

Position: BI Analyst

The problem is that foreach is multiplying the results by the number of categories of the curriculum, the certain would be:

Name: Rodrigo Fontes Santos

Curriculum: Test

Term ID: ["55", "56", "60"]

Position: Network Administrator, Systems Administrator, BI Analyst.

How do I stop foreach from multiplying the results?

    
asked by anonymous 21.02.2017 / 18:52

1 answer

2

Some of these tables you gave join are causing this duplicity, you need to find out what it is and use group by correctly.

To find out, it's interesting that you add in the SELECT the primary key of each LEFT JOIN table and see which column is different for each row, this column will be responsible for duplicity, so you will have to remove it from SELECT and maybe add the other columns to group by .

    
21.02.2017 / 19:04