Support

Account

Home › Forums › General Issues › Need all custom field entries comma delimeted from all custom post type entries

Solved

Need all custom field entries comma delimeted from all custom post type entries

  • So, I have a custom post type called “locations”. In each “location” I ask for their facebook ID. I need to grab all facebook IDs from each published location and output them, separated by commas, for a multifeed display.

    This multifeed is in my site footer, so I can’t limit this to an archive page.

    Help?

  • You need to query all of the locations posts, loop through them and get the values.

    
    $args = array(
      'post_type' => 'locations',
      'posts_per_page' => -1
    )
    $query = new WP_Query($args);
    $facebook_ids = array();
    if ($query->have_posts()) {
      while($query->have_post()) {
        $query->the_post();
        $facebook_ids[] = get_field('facebook_id');
      }
      wp_reset_postdata()
    }
    echo implode(',', $facebook_ids);
    
  • Nope. This didn’t solve it (and there were some missing closing semi-colons).

    I also tried with “explode” instead of “implode” and it didn’t work.

    Here’s my current code:

    $args = array(
      'post_type' => 'locations',
      'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    $facebook_ids = array();
    if ($query->have_posts()) {
      while($query->have_post()) {
        $query->the_post();
        $facebook_ids[] = get_field('facebook_group_name');
      }
      wp_reset_postdata();
    }
    echo explode(',', $facebook_ids);
  • sorry about the typos, what are the results of this, anything?

    
    $args = array(
      'post_type' => 'locations',
      'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    $facebook_ids = array();
    if ($query->have_posts()) {
      while($query->have_post()) {
        $query->the_post();
        $facebook_ids[] = get_field('facebook_group_name');
      }
      wp_reset_postdata();
    }
    echo implode(',', $facebook_ids);
    
  • Is the post type slug correct? 'post_type' => 'locations' Either the query is not returning any posts or there are no values stored for that field name.

  • It is correct, and I’ve double-checked to see that the 3 posts in that CPT each do have that field filled out.

  • So then this query should return 3 posts

    
    $args = array(
      'post_type' => 'locations',
      'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    
    // output posts to see if they are returned by the query
    echo '<pre>'; print_r($query->posts); echo '</pre>';
    
  • Where is this code running? In a template, or in a function.

    try adding

    
    if ($query->have_posts()) {
      global $post; // try adding this here
      while($query->have_post()) {
    
  • In the footer template

  • what kind of field is ‘facebook_group_name’ agian?
    Then that shouldn’t be it.

    What happens when you do this?

    
    $args = array(
      'post_type' => 'locations',
      'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    $facebook_ids = array();
    if ($query->have_posts()) {
      while($query->have_post()) {
        $query->the_post();
        echo get_field('facebook_group_name').'<br />';
        $facebook_ids[] = get_field('facebook_group_name');
      }
      wp_reset_postdata();
    }
    echo implode(',', $facebook_ids);
    
  • facebook_group_name is a simple text field.

    The most recent code you provided displays zip, zero, zilch, nada.

  • k, so there’s an obvious typo in my code, proof that the brain sees what you expect to see since I missed it at least 4 times. Tell me if you can spot the difference

    
    $args = array(
      'post_type' => 'locations',
      'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    $facebook_ids = array();
    if ($query->have_posts()) {
      while($query->have_posts()) {
        $query->the_post();
        echo get_field('facebook_group_name').'<br />';
        $facebook_ids[] = get_field('facebook_group_name');
      }
      wp_reset_postdata();
    }
    echo implode(',', $facebook_ids);
    
  • D’OH!!! A missing ‘S’!! That did it! Thank you! Can’t believe I didn’t see it either. I had to look HARD for that one! LOL

Viewing 15 posts - 1 through 15 (of 15 total)

The topic ‘Need all custom field entries comma delimeted from all custom post type entries’ is closed to new replies.