Skip to main content
added 14 characters in body
Source Link
Ben HartLenn
  • 2.4k
  • 18
  • 19

I will assume that you(or users) will 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 an 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, there's spaces after comma's, or you only entered one id
    {
        if( strpos($arrayIds[0], ',') !== false )// if the first array value has commas, there were spaces after ids entered
        {
            $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
    );
    ...
?>

I will assume that will 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 an 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, there's spaces after comma's, or you only entered one id
    {
        if( strpos($arrayIds[0], ',') !== false )// if the first array value has commas, there were spaces after ids entered
        {
            $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
    );
    ...
?>

I will assume that you(or users) will 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 an 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, there's spaces after comma's, or you only entered one id
    {
        if( strpos($arrayIds[0], ',') !== false )// if the first array value has commas, there were spaces after ids entered
        {
            $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
    );
    ...
?>
added 402 characters in body
Source Link
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'swill assume youthat will 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 simplifiedan 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, there's spaces after comma's, or you only entered one id
    {
        if( strpos($arrayIds[0], ',') !== false )// if the first array value has commas, there were spaces after ids entered
        {
            $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...

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...

I will assume that will 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 an 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, there's spaces after comma's, or you only entered one id
    {
        if( strpos($arrayIds[0], ',') !== false )// if the first array value has commas, there were spaces after ids entered
        {
            $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
    );
    ...
?>
added 402 characters in body
Source Link
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 ALWAYS enter post ids into a posts custom field comma separated, with no spaces, 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...

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 ALWAYS enter post ids into a posts custom field comma separated, with no spaces, eg: 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

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

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...

Source Link
Ben HartLenn
  • 2.4k
  • 18
  • 19
Loading