WordPress.org

Ready to get started?Download WordPress

Forums

Custom Content Type Manager
get_posts_sharing_custom_field_value (5 posts)

  1. jaybehr
    Member
    Posted 2 years ago #

    Hello,

    The following is yielding code the right post IDs that Im after. However, its just spitting out '87513' when I really need the following: array(8, 75, 13). I need to use the output in a WP query.

    <?php
    $projectposts = get_posts_sharing_custom_field_value('program', 'A360');
    foreach ($projectposts as $p) {
    echo $p['ID'];
    }
    ?>

    Thanks!

    Jay

    http://wordpress.org/extend/plugins/custom-content-type-manager/

  2. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    Yep, your code is just printing out the values, one right after the other. If you want an array, you have to feed the values into an array.

    <?php
    $projectposts = get_posts_sharing_custom_field_value('program', 'A360');
    $my_array = array();
    foreach ($projectposts as $p) {
        $my_array[] = $p['ID'];
    }
    print_r($my_array); // <-- now the values are in an array
    ?>
  3. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    Actually... the more I think about this... you've already DONE a query. Full disclosure: I loathe WP_Query (which is one of the main reasons I wrote the Summarize Posts plugin and the integrated GetPostsQuery class) so take this with a grain of salt. By running get_posts_sharing_custom_field_value() you already have all the information about those posts -- every field in them, including the custom fields. Why would you need to do another query just to retrieve some of the same information you already have?

  4. jaybehr
    Member
    Posted 2 years ago #

    Thanks so much for the help. I fear I lack some php fundamentals. (Its really easy to find super basic php instruction or very advanced stuff. The intermediate stuff is under explained.)

    Here's why I was thinking I needed to do the query:

    My user's make posts associated with a to a 'Project' post type.
    Projects are mapped to a Program value (custom field)

    I needed to show all the posts for all projects associated with a program. So I was trying to get all the project ID's associated with a program. Then I'm feeding that array of project ID's into a WP_querry to return the posts.

    Is there a better way?

    Again, thanks greatly!

  5. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    That's what I'm saying: you ALREADY RETURNED THE POSTS. You're adding an unnecessary step. get_posts_sharing_custom_field_value() returns the posts already.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic