WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How would you write this query (4 posts)

  1. mantone
    Member
    Posted 6 years ago #

    I'm trying to write a plugin, and I'm having a hard time going about it.

    How would your write a query that searches the (POSTS)table for (ID) that contain post_mime_type of the value "image/jpeg"

    I want to return the result into a
    <li> list of some sort. One where I can possibly then display on a potential page of some sort.

  2. Aleister
    Member
    Posted 6 years ago #

    Try something like this:

    global $wpdb;
    
    $tp = $wpdb->prefix;
    
    $results = (array)$wpdb->get_results("
    	SELECT ID, post_title
    	FROM {$tp}posts
    	WHERE post_mime_type = 'image/jpeg'
    	ORDER BY post_title
    ");
    
    if (count($results) > 0) {
    	echo '<ul>';
    	foreach ($results as $r) {
    		echo '<a href="' . get_permalink($r->ID) . '">' . $r->post_title . '</a>';
    	}
    	echo '</ul>';
    
    }

    (I just typed that out, so there may be bugs, etc.. but it should give you an idea at least)

  3. mantone
    Member
    Posted 6 years ago #

    Thanks, that was really helpful

  4. Aleister
    Member
    Posted 6 years ago #

    No problem :)

Topic Closed

This topic has been closed to new replies.

About this Topic