0

So I am trying to get custom post values in WP. The thing is I can have multiple custom fields value. So I don't want to hard code in the code. What I am thinking of doing is providing the a prefix in the key value something like

abc_email
abc_website

So I want to use this function

get_post_meta

or some other to get all the key,value pair that starts with abc. That way I can just add values on the back end and don't have to update my code. One way would be to get all the meta data using above function and then loop over it and filter. But is there any other way where I can send in the pattern I am looking for?

Thanks

1

2 Answers 2

1

You could directly query the database for a posts entries containing the prefix (relevant table etc here https://wordpress.stackexchange.com/questions/104434/where-are-custom-field-values-stored-in-the-database )

  • but this is more complex, still involves a loop and doesn't provide you with anything you could do using WP and PHP. If you want to cater for unknowns you will have to loop or interogate an index etc.

I may be missing something in your question but this seems straight forward:

You are trying to avoid this scenario:

process_field($abc_known1);
process_field($abc_known2);

// continually edit code for new entries e.g.:
process_field($abc_NEW1);

Something like this will handle later additions without modification.

//  get array of ALL cust fields for post
$all_cust_fields = get_post_meta( get_queried_object_id());
foreach ($all_cust_fields as $key => $value) {
  // some function for determing if $value starts with "abc_"  
  // if so either:
  //     add to an "abc" array for later use/processing
  // or take action in this loop e.g.

   process_field($value);
}

I am not sure whether you are talking about keys or values prefixed "abc_" but the same principle applies.

Sign up to request clarification or add additional context in comments.

Comments

0

you should write code like this,

$email_meta = get_post_meta( 'your_post_id', 'abc_email', true );
$website_meta = get_post_meta( 'your_post_id', 'abc_website', true );

Or

you can follow these links,

example

example

Hope this will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.