Home > Enterprise >  How to skip a meta key?
How to skip a meta key?

Time:11-08

Var_dump shows

array (
  'media' => 
  array (
    0 => '327',
  ),
  'content' => 
  array (
    0 => '
Custom test 1

',
    1 => '
Custom test 2

',
  ),
  '_edit_lock' => 
  array (
    0 => '1667908749:1',
  ),
)

but I don't want _edit_lock

I've tried:

$fields = get_post_meta($postId);
echo '<ul>';
foreach($fields as $i) {
    foreach($i as $key => $value) {
        if(in_array($key,array("_edit_lock", "_edit_last", "_thumbnail_id"))) continue;
        echo '<li>'.$value.'</li>';
    }       
}
echo '</ul>';

CodePudding user response:

Eventually I removed them directly from the array using unset:

$fields = get_post_meta($postId);
unset($fields['_edit_lock'], $fields['_edit_last'], $fields['_thumbnail_id']);

CodePudding user response:

Yes, and your algorithm is correct. But would it be better if you didn't use the first loop?
Like this:

foreach ((array) get_post_meta($postId) as $key => $arrayValue) {
    if (!in_array($key, array("_edit_lock", "_edit_last", "_thumbnail_id"))) {
        foreach ($arrayValue as $key => $value) {
            echo '<li>' . $value . '</li>';
        }
    }
}

CodePudding user response:

You are checking your key in the wrong level of the array.

$fields = array (
  'media' => 
  array (
    0 => '327',
  ),
  'content' => 
  array (
    0 => '
Custom test 1

',
    1 => '
Custom test 2

',
  ),
  '_edit_lock' => 
  array (
    0 => '1667908749:1',
  ),
);
echo '<ul>';
foreach($fields as $key => $i) {
    if(in_array($key,array("_edit_lock", "_edit_last", "_thumbnail_id"))) continue;
    foreach($i as $value) {
        echo '<li>'.$value.'</li>';
    }       
}
echo '</ul>';

Demo : https://3v4l.org/X8QW6

  • Related