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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:44:41+00:00 2026-06-05T23:44:41+00:00

I have a table which stores the data related to posts in this format

  • 0

I have a table which stores the data related to posts in this format

describe wp_postmeta;

+------------+---------------------+------+-----+---------+----------------+
| Field      | Type                | Null | Key | Default | Extra          |
+------------+---------------------+------+-----+---------+----------------+
| meta_id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| post_id    | bigint(20) unsigned | NO   | MUL | 0       |                |
| meta_key   | varchar(255)        | YES  | MUL | NULL    |                |
| meta_value | longtext            | YES  |     | NULL    |                |
+------------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

The meta_key has a key like 'author', 'excerpt', 'download'. Meta value has corresponding values.

Now I am writing a php script that queries the DB based on a post_id. SO I need to display all the meta_data from the above table in an order specified. The query is:

    $select_post_meta="select meta_value from wp_postmeta where post_id='".$postid."' and (meta_key='author' or meta_key='category' or meta_key = 'excerpt')";

$meta_paper = mysql_query($select_post_meta);

    while(($meta_values = mysql_fetch_array($meta_paper))
            {   echo "<i>'".$meta_values['meta_value']."'</i><br/>";  }

It would be really nice if the meta values are displayed in the same order as I queried but they are positioned as first description, category and Author. Is there any other way I can query database so that I can get values in that order which is, first Author, Category, Description. Thank you

  • 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-05T23:44:43+00:00Added an answer on June 5, 2026 at 11:44 pm

    Add

    ORDER BY meta_key
    

    to your query text. Like this:

    $select_post_meta="select meta_value from wp_postmeta where post_id='".$postid."'
    and (meta_key='author' or meta_key='category' or meta_key = 'excerpt')
    ORDER BY meta_key";
    

    I strongly suggest that you thwart SQL injection by avoiding the inclusion of “unsafe” strings in your SQL text, e.g.

    $select_post_meta = sprintf("select meta_value from wp_postmeta 
    where post_id='%s'
    and (meta_key='author' or meta_key='category' or meta_key = 'excerpt')
    ORDER BY meta_key", mysql_real_escape_string($postid));
    

    It just happens that you listed the search terms in alphabetical order, which makes the ORDER BY expression pretty simple.

    If you needed them in some other order, you could use a different expression.

    ORDER BY CASE meta_key WHEN 'Author' THEN 1 WHEN 'Category' THEN 2 WHEN 'Excerpt' THEN 3 END
    

    We also note that your query might return any number of values for each key (given that we don’t see a unique constraint on (post_id, meta_key).

    It’s possible that the query could return zero, one or more values for Author, Category and Excerpt, and you have no way of knowing which is which.

    My tendency here would be to include meta_key in the result set as well. Then my code would be able to tell that a particular meta_value was for a particular meta_key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql table field set as time type which stores data in
I have a table which stores test results like this: user | score |
So I have a reference table which stores the primary key, description and update
I have a table:'Categories' which has two fields:Category_ID and Category. Data in Category_ID field
I have created an archive table which will store data for selecting only. Daily
I have a table from which I query data to display resultset using stored
Imagine I have a table which stores a series of sparse vectors. A sparse
(I am using PostgreSQL) I have a table which stores transactions to an account.
While working with ActiveRecord I have a table which stores a serialized array of
I have a MYSQL-Table which stores scores. Every time a user improves a new

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.