WordPress.org

Ready to get started?Download WordPress

Forums

How to retrieve posts in a certain category with PHP (3 posts)

  1. rx8wej
    Member
    Posted 5 years ago #

    I use wordpress as the back-end for my news posts on my main site. I post updates to my software via posts in a certain category. I want to be able to display a list of posts that were posted in a specific category on a page that is not part of WordPress. I already do this for the front page news posts, but I can't figure out how to limit the SQL select results based on a certain category. I thought about just starting every post with a certain word and doing a LIKE "word%" but I rather actually do a "select * from table where category=#" if possible. All the category #s in the posts table are set to 0 so I have no idea how WordPress is able to know what post belongs to what category.

    Is there an easy way to do this? I did a little googling but I can't find anything relevant.

  2. rx8wej
    Member
    Posted 5 years ago #

    I think I figured it out. I manually figured out what taxonomy_id I needed. Here is how I did it:

    $sql = "select * from 'wp_term_relationships' where 'term_taxonomy_id'= $cat order by 'object_id' desc limit 5";
    $result = mysql_query($sql) or die(mysql_error());
    echo "
    
    <ul>";
    while($row = mysql_fetch_array($result))
    {
    	$sql2 = "select * from 'wp_posts' where 'ID' = " . $row['object_id'] . "";
    	$result2 = mysql_query($sql2) or die(mysql_error());
    
    	while($row2 = mysql_fetch_array($result2))
    	{
    		$date = date("M jS, Y", strtotime($row2['post_date']));
    
    		echo "
    
    <li><a>", $row2['post_title'], "</a> - ", $date, "</li>
    ";
    	}
    
    }

    Is this the best way to do this? Or is there a better way?

  3. mores
    Member
    Posted 5 years ago #

    there definately is a prettier way to do it, but I don't know how :)
    Something with an SQL command like LEFT JOIN or RIGHT JOIN or INNER JOIN, that'll let you combine your two SQL selects into one clean call.

Topic Closed

This topic has been closed to new replies.

About this Topic