Michael M. Isaac
Michael M. Isaac

Reputation: 29

get post data from wordpress by sql query

I want to get wordpress post data and get wp_attached_file from wp_postmeta table this is my sql query

$query= mysql_query("SELECT wp_posts.ID,wp_posts.post_title,wp_posts.post_date,wp_postmeta.meta_value 
                FROM wp_posts

                  JOIN wp_term_relationships
                        ON wp_term_relationships.object_id = wp_posts.ID

                    JOIN wp_postmeta
                        ON wp_postmeta.post_id = wp_posts.ID

                WHERE wp_posts.post_date > '$before7' 
                AND wp_posts.post_status = 'publish' 
                AND wp_posts.post_type = 'post' 
                AND wp_term_relationships.term_taxonomy_id = '$cat' 
                AND wp_postmeta.meta_key = '_wp_attached_file' 
                ORDER BY wp_posts.post_date DESC LIMIT 10");

it give me nothing but if I removed this line from where clause

AND wp_postmeta.meta_key = '_wp_attached_file'

it works but I need this line to get wp_attached_file

so what is wrong with mysql query

Upvotes: 0

Views: 1622

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133370

Be sure that the wp_postmeta.meta_key with the value _wp_attached_file really exist and then could be there is some problem for match try trim both cause hidden char

  AND trim(wp_postmeta.meta_key)  = trim('_wp_attached_file')

Upvotes: 1

Related Questions