• I’m having a little trouble figuring out how to retrieve an array of post excerpts by the category ID.

    I don’t really understand the taxonomy, terms, and terms relationships, so I’m having difficulty writing a query that pulls the post excerpt by category ID.

    Any help would be greatly appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Not sure if this is exactly what you’re looking for, but the query below will pull out all of your posts from a specific category ID:

    SELECT DISTINCT ID, post_title, post_name, guid, post_date, post_content
    FROM wp_posts AS p
    INNER JOIN wp_term_relationships AS tr ON (
    p.ID = tr.object_id
    AND tr.term_taxonomy_id
    IN ( 1 )
    )
    INNER JOIN wp_term_taxonomy AS tt ON (
    tr.term_taxonomy_id = tt.term_taxonomy_id
    AND taxonomy = 'category'
    )
    ORDER BY id DESC

    Just replace the “1” with the ID of the category you’re trying to filter by.

    -Manoj

    manoj,

    there is some minor correction in your query.

    SELECT DISTINCT ID, post_title, post_name, guid, post_date, post_content
    FROM wp_posts AS p
    INNER JOIN wp_term_relationships AS tr ON (
    p.ID = tr.object_id
    
    )
    INNER JOIN wp_term_taxonomy AS tt ON (
    tr.term_taxonomy_id = tt.term_taxonomy_id
    AND taxonomy = 'category' AND tt.term_id
    IN ( 16 )
    )
    ORDER BY id DESC
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘MySQL query by category ID’ is closed to new replies.