Getting posts from wordpress returns the same result when present in several cathegories

In the home page of a website I need to return the last 3 posts of the blog, created with wordpress.

I have this code:

SELECT p.post_title, p.post_date, p.post_content, wpr.object_id, dt_blog_terms.name,        dt_blog_terms.slug
FROM dt_blog_terms
INNER JOIN dt_blog_term_taxonomy ON dt_blog_terms.term_id = dt_blog_term_taxonomy.term_id
INNER JOIN dt_blog_term_relationships wpr ON wpr.term_taxonomy_id = dt_blog_term_taxonomy.term_taxonomy_id
 INNER JOIN dt_blog_posts p ON p.ID = wpr.object_id
 WHERE taxonomy = 'category'
 AND p.post_type = 'post'
 AND p.post_status = 'publish'
 AND slug != 'notizie-notifiche'
 ORDER BY `post_date` DESC
 LIMIT 3

As you can see, I have one cathegory (notizie-notifiche) that I want to exclude. This sql string works, but I have a problem when the post has more than one cathegory. In this case, it is returned once for every cathegory, while I want to show it only once in total.

Any idea? I thought I could use DISTINCT, but it does not seem to work with this kind of SELECT statement.

Solutions

I can tell you what you need to do, but I can't modify the query to do it. You use the term "category" in your question, but there is no field by that name in your data.

You need to aggregate your data, at the level you want, and then include a having clause. The following group by may solve your problem:

group by p.id
having sum(case when slug = 'notizie-notifiche' then 1 else 0 end) = 0

Similar questions

Custom posts listing widget always returns 1 extra result
I've built my own custom 'recent posts' widget to display some more content than the default core WP recent posts widget does. I'm struggling with controlling my list of entries. Currently, the widget always returns 1 additional post to the list selected. So if I select 1 post to show, it will actually show 2, and so forth. I'm not really sure wher...
get_next_posts_link() returns no result despite available posts
I am developing a custom theme based on the Genesis framework. I want to display a 'previous post | next post' navigation below any article in the single post view. However, neither get_next_posts_link() nor get_previous_posts_link() return any value. All output I can see is | Of course, there are adjacent posts available. My code looks something l...
Running several WordPress sites on same core / plugins?
Is it possible to run more than one site on the same WordPress core and a set of plugins? I want to be able to have custom plugins and themes for each site, but have a base set that is the same, to make it easier to maintain.
WordPress - Several Menulinks to same URL active
Im designing a big Dropdown-Menu with 2 Sub-Levels in the Divi WordPress Theme, where several menu-links are individual-links pointing to the same Page (the customer wants this). Now all of those links get an active class by default in WordPress once any link is clicked. Is there an easy way around this without using jQuery? I tried solving this us...
Add several fields from different tables to post, fields per post are in same row of table
I like to add 4 or 5 extra fillout boxes to the adminpage of my post... For example, under the actual page box, I would like to add the following fields: Here is the problem, I want to save this data upon saving the post/page but it should be saved to a separate table and in the same row. So the extra table has fields: I can not use the custom fiel...
Widget with same content across several websites
I'm managing a couple of websites from the same client. They have lots of partners and have a logo slider on the bottom of each page across 5 websites. The partners change from time to time and the content manager has to update the logos on 5 sites separately. Maybe there's a way so that you can change the logos one time and the other sites change ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.