0

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

2
  • I think you're using the default wordpress database's prefix, but the right way to refere a table in a SQL request is this de variable $wpdb->prefix, like this : "SELECT ".$wpdb->prefix."posts.ID,".$wpdb->prefix."posts.post_title ...." Commented Oct 23, 2016 at 21:49
  • You are querying for post_type = 'post' but if I remember right, _wp_attached_file is only used with post_type = 'attachment' Commented Oct 24, 2016 at 0:10

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.