Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8948033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:54:09+00:00 2026-06-15T12:54:09+00:00

I am trying to get the wp posts using custom query. Here is sql

  • 0

I am trying to get the wp posts using custom query. Here is sql query:

SELECT p.*, IFNULL(SUM(vote), 0) AS vote_count, CAST(m.meta_value AS SIGNED) AS idea_count
FROM wp_posts p
INNER JOIN wp_term_relationships r ON p.ID=r.object_id
INNER JOIN wp_term_taxonomy t ON t.term_taxonomy_id=r.term_taxonomy_id AND t.taxonomy='category'
LEFT JOIN wp_wdpv_post_votes v ON v.post_id=p.ID
LEFT JOIN wp_postmeta m ON m.post_id=p.ID AND m.meta_key='ideas_count'
WHERE p.post_status='publish' AND p.post_type='post' AND t.term_id='5'
GROUP BY p.ID
ORDER BY p.ID DESC
LIMIT 0, 8

This sql query is working fine. But now i have another case, Where I want show the posts using this query but only the posts which does not have specific meta key. Meta key to filter is

'private_spaces_post'

This could be specific to mysql query. But i will be very thankful if someone can give me a solution for this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:54:10+00:00Added an answer on June 15, 2026 at 12:54 pm

    You can LEFT JOIN against a subquery which only returns those that do have private_spaces_post, and look for NULLs

    SELECT p.*, IFNULL(SUM(vote), 0) AS vote_count, CAST(m.meta_value AS SIGNED) AS idea_count
    FROM wp_posts p
      INNER JOIN wp_term_relationships r ON p.ID=r.object_id
      INNER JOIN wp_term_taxonomy t ON t.term_taxonomy_id=r.term_taxonomy_id AND t.taxonomy='category'
      LEFT JOIN wp_wdpv_post_votes v ON v.post_id=p.ID
      LEFT JOIN wp_postmeta m ON m.post_id=p.ID AND m.meta_key='ideas_count'
      /* LEFT JOIN subquery returning only ids that *do* have the meta key */
      LEFT JOIN (
        SELECT post_id FROM wp_postmeta WHERE meta_key='private_spaces_post'
      ) psp ON p.ID = psp.post_id
    WHERE p.post_status='publish' AND p.post_type='post' AND t.term_id='5'
      /* And find post ids from the main table that *don't* have a match in the subquery (LEFT JOIN returns NULL) */
      AND psp.post_id IS NULL
    GROUP BY p.ID
    ORDER BY p.ID DESC
    LIMIT 0, 8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$this->db->from('posts'); $this->db->where('id',$this->uri->segment(3)); $data['query'] = $this->db->get(); I am trying to get one post using a
I'm trying to create a custom module and I'm trying to get MY SQL
I'm trying to get a custom query going in wordpress. Basically, I want to
I'm trying to get the error message for a field using a custom validation
I'm stuck trying to get a WinInet HTTP POST via SSL using ONLY C.
I am trying to get my posts tagged with a certain tag to render.
I am trying to get the number of comments from each of the posts
I am trying to get some form data from POST method. Here's the code
I'm using WSGI and trying to access the get/post data, using this code: import
Trying to get Nunit working in ASP.Net. The problem is, I'm testing a custom

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.