Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

somtam on "get the attachment only if the post status of the parent is published"

$
0
0

Hello everybody.

I have to made an attachment archive by custom taxonomy.
I don't know why the page of the term, like mysite.com/custom_taxonomy/attachment_term, doesn't work.

Anyway I decide to write a custom query by myself and run it inside a template page.
It works, but I have a problem.
When I get the attachments, I get all the of them, even if the parent is not published. This cause a 404 if someone clicks on them. Instead, if the post_parent is 0, with a click we arrive to the attachment page mysite.com/?attachment_id=x. Moreover if the attachment is regularly inside a post, it goes to mysite.com/post_name/attachment.

So how can I check all of these and get the attachments with parent 0 or the parent Publish?

Here's the query right now

$sql = "
	SELECT p.*
	FROM $wpdb->posts p
	LEFT JOIN $wpdb->term_relationships r
		ON r.object_id = p.ID
	LEFT JOIN $wpdb->term_taxonomy t
		ON t.term_taxonomy_id = r.term_taxonomy_id
	LEFT JOIN $wpdb->terms te
		ON te.term_id = t.term_id
	WHERE te.slug = 'OneOfmMyTerms'
";

thanks!!!


Viewing all articles
Browse latest Browse all 8245

Trending Articles