0
How to do for foreach do not multiply the results coming 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 consultation is as follows::
Name: Rodrigo Fontes Santos
Curriculum: Test
Term ID: ["55","56","60"]
Position: Administrator of Networks
Name: Rodrigo Fontes Santos
Curriculum: Test
Term ID: ["55","56","60"]
Position: System Administrator
Name: Rodrigo Fontes Santos
Curriculum: Test
Term ID: ["55","56","60"]
Position: BI Analyst
The problem is that the foreach
is multiplying the results by the number of categories of the curriculum, the right would be:
Name: Rodrigo Fontes Santos
Curriculum: Test
Term ID: ["55","56","60"]
Position: Network Administrator, Systems Administrator, BI Analyst.
How to make foreach not multiply results?
Even though it’s not the focus of the question, your algorithm is very bad. Buddy, never select or any other interaction with the database within a loop.
– Cesar Augusto
@Cesar, thanks for Feedback, I’m new to the back end.
– Rodrigo Fontes