• Resolved gafa

    (@gafa)


    Hi for one page, I try to use a $wpdb->get_results() with a complex query (3 joints on the standard tables). The query runs directly on mySql, returns 2 results as expected, but $wpdb returns nothing !
    My php code :

    $wpdb->flush();
    $request = 'SELECT posts.ID AS ID, posts.post_title AS post_title, posts.post_content AS post_content
    FROM '.$wpdb->posts.' posts
    LEFT JOIN '.$wpdb->term_relationships.' relationships ON relationships.object_id = posts.ID
    LEFT JOIN '.$wpdb->term_taxonomy.' taxonomy ON taxonomy.term_taxonomy_id = relationships.term_taxonomy_id
    LEFT JOIN '.$wpdb->terms.' terms ON terms.term_id = taxonomy.term_id
    WHERE terms.name = "'.$pageTerm.'"
    AND posts.post_type = "reference"
    AND posts.post_status = "publish"
    ORDER BY posts.post_date DESC
    LIMIT 0, 2;';
    $references = $wpdb->get_results($request);
    The request generated is :
    SELECT posts.ID AS myID, posts.post_title AS myPost_title, posts.post_content AS myPost_content FROM wp1_posts posts LEFT JOIN wp1_term_relationships relationships ON relationships.object_id = posts.ID LEFT JOIN wp1_term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = relationships.term_taxonomy_id LEFT JOIN wp1_terms terms ON terms.term_id = taxonomy.term_id WHERE terms.name = "Restaurant" AND posts.post_type = "reference" AND posts.post_status = "publish" ORDER BY posts.post_date DESC LIMIT 0, 2;

    This request returns 2 result in phpMyAdmin as execpted.

    The return of get_results() is empty !!!

    I already use $wpdb->get_results on other pages but with the request with only one joint.
    Is it the problem ?

    If anybody have an idea, feel free to help.

Viewing 1 replies (of 1 total)
  • Thread Starter gafa

    (@gafa)

    When I tryed to debug I use
    exit( var_dump( $wpdb->last_query ) );
    prefer
    $wpdb->show_errors(); at the beginning. This help me !

    In my case I get $pageTrem used in the request with
    get_the_term_list($post->ID,’secteur’);
    (secteur is a taxonomy like Restaurant, club, …)
    but this return the term name html fomatted and in fact the request was :
    ... WHERE terms.name = "<a href="https://192.168.192.244/ha/?secteur=restaurant" rel="tag">Restaurant</a>"" ...

    I replace by
    wp_get_object_terms($post->ID,'secteur');
    this function permit to get the term_id more efficient for my request.

    Losing time for nothing ??

Viewing 1 replies (of 1 total)
  • The topic ‘The limitation or error of $wpdb’ is closed to new replies.