Forums

MySQL query by category ID (3 posts)

  1. freshifreshy
    Member
    Posted 1 year ago #

    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!

  2. manoj382
    Member
    Posted 1 year ago #

    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

  3. cglance08
    Member
    Posted 10 months ago #

    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

    Wordpress Customization

Topic Closed

This topic has been closed to new replies.

About this Topic