How to query more than one table (Wordpress)

Asked

Viewed 302 times

0

How do I perform a QUERY in the database in 5 different tables and return the values I want, example:

I am performing a search system (my first) and need to capture the following values: Candidate Name, Resume Title and Vacancy Category. Now let’s get the information:

  • The candidate’s name is in the table wp_users;
  • The title of the curriculum is in the table wp_posts;
  • The category of the vacancy is in the table wp_terms;
  • wp_users relates directly to wp_posts;
  • wp_posts relates directly to wp_term_relationships;
  • wp_term_relationships relates directly to wp_term_taxonomy;
  • wp_term_taxonomy relates directly to wp_terms;
  • But there is one condition... A query must be made in the table wp_usermeta where meta_value = '_jm_candidate_field_clocknow_user_btn' and meta_key = 'value_1';
  • The table wp_usermeta is directly related to wp_users

wp_usermeta -> wp_users -> wp_posts -> wp_term_relationships -> wp_term_taxonomy -> wp_terms

inserir a descrição da imagem aqui

The query I’m doing is not returning values:

$resultados = $wpdb->get_results( "SELECT $wpdb->users.display_name, $wpdb->posts.post_title, $wpdb->terms.name, $wpdb->usermeta.meta_key, $wpdb->usermeta.meta_value 

FROM $wpdb->usermeta 

WHERE $wpdb->usermeta.meta_key = '_jm_candidate_field_clocknow_user_btn' AND $wpdb->usermeta.meta_value = 'value_2' 

INNER JOIN $wpdb->users ON $wpdb->usermeta.user_id = $wpdb->users.ID 

INNER JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID 

INNER JOIN $wpdb->term_relationships ON $wpdb->term_relationships.object_id = $wpdb->posts.ID 

INNER JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->term_relationships.term_taxonomy_id 

INNER JOIN $wpdb->terms ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_taxonomy_id;" );

Would it be logical that this wrong?

  • Just do an Inner Join.

  • Only using INNER JOIN in a query is possible?

  • Possibly yes, I would need more details (and better know the structure of the bank) to say for sure.

2 answers

1

SELECT Nome, Titulo, Categoria FROM wp_users
INNER JOIN wp_posts ON wp_users.id = wp_posts.id
INNER JOIN wp_term_relationships ON wp_term_relationships.id = wp_posts.id
INNER JOIN wp_term_taxonomy ON wp_term_taxonomy.id = wp_term_relationships.id
INNER JOIN wp_terms ON wp_terms.id = wp_term_taxonomy.id;

See if this code works. Just remember that I don’t even know how the tables look, so replace the ids with the respective table columns.

  • It worked yes, thank you! But I forgot a detail... has a condition that must be made... In the table wp_usermeta a check shall be made where meta_value = '_jm_candidate_field_clocknow_user_btn' and meta_key = 'value_1'. The wp_usermeta table is directly related to wp_users. Here is a picture of the list of tables (https://codex.wordpress.org/images/2/25/WP4.4.2-ERD.png)

-3

as it would be for a pagination in this result list I did a foreach that repeats the records, but if it has say 100 results, how to divide into 10 pages with pagination navigation links?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.