Skip to main content
2 of 4
added 402 characters in body
Ben HartLenn
  • 2.4k
  • 18
  • 19

There is a way to do this that I can think of, but it does leave potential for error in the custom field data entry.

Let's assume you enter post ids into a posts custom field comma separated, eg: 11,13,34,54 OR 11, 13, 34, 54.

Then all you need to do is get the custom field value for the loaded post, explode the custom field value by comma(,): and, then you'll have a nice array to pass into the include parameter. Here is a simplified example showing the important pieces:

<?php
    $postIds = get_post_meta($post->ID, 'postIds', true); // get custom field value
    $arrayIds = explode(',', $postIds); // explode value into an array of ids
    if(count($arrayIds) <= 1) // if array contains one element or less
    {
        $arrayIds = array(); // reset array
        $arrayIds = explode(', ', $postIds); // explode ids with space after comma's
    }

    $args = array(
        'include' => $arrayIds // pass array of ids into `include` parameter
    );
    ...
?>

You could make it more solid with an extra check that if the array has one value, then check if there's commas in the one value, if not, then only one id was entered...

Ben HartLenn
  • 2.4k
  • 18
  • 19