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 8787093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:47:59+00:00 2026-06-13T21:47:59+00:00

I am running a competition which is based on WP Custom Fields. I am

  • 0

I am running a competition which is based on WP Custom Fields. I am trying to count all meta values of a meta_key=’odd’ for a specific user. I am using the formula below but it’s not returning any result.

$meta_key = 'odd';
$post_author = 'Admin';
$odd = $wpdb->get_var( $wpdb->prepare(
    "
    SELECT Count(meta_value)
    FROM $wpdb->postmeta
    INNER JOIN $wpdb->posts ON $wpdb->postmeta->post_id = $wpdb->posts->ID
    WHERE $wpdb->postmeta->meta_key = 'odd' AND $wpdb->posts->post_author = 'Admin'
    ",
    $meta_key, $post_author
) );
echo "<p>Total odd {$odd}</p>";

Anyone can help me? Thanks in advance.

  • 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-13T21:48:00+00:00Added an answer on June 13, 2026 at 9:48 pm

    You are mixing up the $wpdb variables and the $post variables – for your JOIN, for example, you can’t use $wpdb->postmeta->post_id, it would be {$wpdb->postmeta}.post_id, and would be even better to just use table aliases. Your prepare() is also not passing in your meta_key or author because you don’t have %s placeholders for them

    $odd = $wpdb->get_var( $wpdb->prepare(
        "
        SELECT count(meta_value)
        FROM {$wpdb->postmeta} pm
        INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
        WHERE pm.meta_key = %s AND p.post_author = %s
        ",
        $meta_key, $post_author
    ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which models of algorithm running time exist? We all expect mergesort to be faster
I'm running into an odd problem with MEF's composition container and directory catalog where
I'm constructing a geolocation based application and I'm trying to figure out a way
I'm writing an AI-testing Framework for a competition. Participants submit a Bot class which
I'm trying to run the following program, which calculates roots of polynomials of degree
Im coding in php and taking part in a coding competition which gives points
We run an AI programming competition in which contestants will code an AI that
I have a JSF 2.1 (MyFaces) app running using several Session Beans (All the
I am running a Django-based webservice with Gunicorn behind nginx as a reverse proxy.
Motivation I have a long-running boolean function which should be executed in an array

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.