Support » Fixing WordPress » while() and newline (\n), how does it make sense?

  • echo "300-600|Bad Karma
    200
    400|Rose";

    It doesn’t help. I wants to check if the string has a newline. `$activities_label=strstr($activities_label, strpos($activities_label,”PHP_EOL”));
    echo $activities_label;` But doesn’t match. Here is an example:

    $activities_label = get_post_meta(get_the_ID(), "activities_label", true);
    ...
    while($query->have_posts()) :
      ...  
      if (false !== strpos($activities_label, '\n') && false !== strpos($activities_label, PHP_EOL)) echo $activities_label;
    endwhile;

    Newbie.

    //view

    Bad Karma
    300-600|Bad Karma
    200
    400|Rose

    Blue Elephant
    300-600|Bad Karma
    200
    400|Rose

    Rose
    300-600|Bad Karma
    200
    400|Rose

    But I wouldn’t have:

    Bad Karma
    300-600|Bad Karma

    Blue Elephant
    200

    Rose
    400|Rose

    • This topic was modified 2 years, 4 months ago by TheDani.
    • This topic was modified 2 years, 4 months ago by TheDani.
    • This topic was modified 2 years, 4 months ago by TheDani.
Viewing 12 replies - 1 through 12 (of 12 total)
  • can you try this? just switched one of the functions and changed the logical operator.

    $activities_label = get_post_meta(get_the_ID(), 'activities_label', true);
    
    while ($query->have_posts()) { 
    	if (strpos($activities_label, "\n") !== false || false !== strstr($activities_label, PHP_EOL)) {
    	  echo $activities_label;
    	}
    }

    and sorry, but i’m and old fashioned curly brackets kind of guy, not one of those new line, curly brackets guys though…those guys are weird

    Hey,

    WordPress, Pods Admin, Manage Pods, Page:
    Activities
    Label: Activities
    Name: activities
    Field Type: Relationship
    Relates: Activities

    Activities Label
    Label: Activities Label
    Name: activities_label
    Field Type: Plain Paragraph Text
    Output options: empty

    //source single-tmpl.php

    $activities = get_post_meta(get_the_ID(), "activities", false);
    $ids = array_column($activities, 'ID');
    $activities_label = get_post_meta(get_the_id(), 'activities_label', true);
    
    $args = array(
       'post_type' => 'activity',
       'posts_per_page'=>'-1',
       'post__in' => $ids,
       'orderby' => 'post__in',
    );
    $query = new WP_Query($args);
    while($query->have_posts()) :
       $query->the_post();
       echo "<br>".get_the_title();
       
       //$activities_label = preg_replace('/^(.+)(\s*)$/m', '<li>$1</li>', $activities_label);
       if (strpos($activities_label, "\n") !== false || false !== strstr($activities_label, PHP_EOL)) {
          echo "<br>".$activities_label."<br><br>";   
       }
    endwhile;

    View:

    Bad Karma
    300-600|Bad Karma 200 400|Rose
    
    Blue Elephant
    300-600|Bad Karma 200 400|Rose
    
    Rose
    300-600|Bad Karma 200 400|Rose

    🙁

    I have lot of different php views (up), but no one as these (down)

    Bad Karma
    300-600|Bad Karma
    
    Blue Elephant
    200
    
    Rose
    400|Rose

    maybe move the ‘activities_label’ variable INSIDE the loop?

    $activities = get_post_meta(get_the_ID(), "activities", false);
    $ids = array_column($activities, 'ID');
    
    $args = array(
       'post_type' => 'activity',
       'posts_per_page'=>'-1',
       'post__in' => $ids,
       'orderby' => 'post__in',
    );
    
    $query = new WP_Query($args);
    
    while($query->have_posts()) :
    	$query->the_post();
    	$activities_label = get_post_meta(get_the_id(), 'activities_label', true);
    
    	echo "<br>".get_the_title();
       
    	if (strpos($activities_label, "\n") !== false || false !== strstr($activities_label, PHP_EOL)) {
    		echo "<br>".$activities_label."<br><br>";   
    	}
    endwhile;

    No,

    $activities = get_post_meta(get_the_ID(), "activities", false); //activity -ies
    $ids = array_column($activities, 'ID');
    $args = array(
      'post_type' => 'activity',
      'posts_per_page'=>'-1',
      'post__in' => $ids,
      'orderby' => 'post__in',
    );
    $query = new WP_Query($args);
    while($query->have_posts()) :
       $query->the_post();
       $activities_label = get_post_meta(get_the_id(), 'activities_label', true);
       echo "<br>".get_the_title();
       if (strpos($activities_label, "\n") !== false || false !== strstr($activities_label, PHP_EOL)) {
           echo "<br>".$activities_label."<br><br>";
       }
    endwhile;

    //result

    Bad Karma
    Blue Elephant
    Rose
    • This reply was modified 2 years, 4 months ago by TheDani.
    • This reply was modified 2 years, 4 months ago by TheDani.

    Its hard to tell what you have going on so everything I say here is just a wild guess. My next guess would be to remove your conditional statement, for testing purposes, just to make sure something is getting echoed.

    $activities = get_post_meta(get_the_ID(), "activities", false);
    $ids = array_column($activities, 'ID');
    
    $args = array(
       'post_type' => 'activity',
       'posts_per_page'=>'-1',
       'post__in' => $ids,
       'orderby' => 'post__in',
    );
    
    $query = new WP_Query($args);
    
    while($query->have_posts()) :
    	$query->the_post();
    	$activities_label = get_post_meta(get_the_id(), 'activities_label', true);
    
    	echo "<br>".get_the_title()."<br>".$activities_label."<br><br>";
    
    endwhile;

    please show me what happens with that.

    Hey, no. It shows same result. Show $activities, but no $activities_label.

    Bad Karma
    Blue Elephant
    Rose

    Ok. I looked a little longer at what I think you are trying to do.

    Get all post from custom post type ‘activity’ that HAVE a post meta value?

    Maybe just change the values of your query first?

    <?php
    
    $args = array(
       'post_type' => 'activity',
       'posts_per_page'=>'-1',
       'meta_key' => 'activities_label',
       'compare' => 'EXISTS'
    );
    
    $query = new WP_Query($args);
    
    while($query->have_posts()) :
    	$query->the_post();
    	$activities_label = get_post_meta(get_the_id(), 'activities_label', true);
    
    	echo "<br>".get_the_title()."<br>".$activities_label."<br><br>";
    
    endwhile;

    No. This has two records: activity and post.activities_label (and post.activities)

    
    'post_type' = 'activity',  //activity
    'posts_per_page' = '-1',
    'meta_key' = 'activities_label', //post.activities_label
    'compare' = 'EXISTS'
    

    Edit Pod – Manage Fields (Pods Admin):
    * post – activities (relationship – activity) and activities_label (plain paragraph text).
    * activity – facilities (relationship – simple custom defined list)

    WordPress, Pods Admin, Manage Pods, Post

    Activities
    Label: Activities
    Name: activities
    Field Type: Relationship
    Relates: Activities

    Activities Label
    Label: Activities Label
    Name: activities_label
    Field Type: Plain Paragraph Text
    Output options: empty

    WordPress, Pods Admin, Manage Pods, Activity

    Facilities
    Label: facilities
    Name: facilities
    Field Type: Simple Custom defined list
    Custom Defined Options: a, b, c, d, e, f, g, etc

    But later, facilities.

    
    $args = array(
       'post_type' => 'activity',
       'posts_per_page'=>'-1',
       'meta_key' => 'activities_label',
       'compare' => 'EXISTS'
    );
    $query = new WP_Query($args);
    while($query->have_posts()) :
    	$query->the_post();
    	$activities_label = get_post_meta(get_the_id(), 'activities_label', true);
    	echo "<br>".get_the_title()."<br>".$activities_label."<br><br>";
    endwhile;

    View:
    ,
    🙁

    • This reply was modified 2 years, 4 months ago by TheDani.
    • This reply was modified 2 years, 4 months ago by TheDani.

    ok. it seems you are using a plugin called PODS. I would recommend looking at their documentation as they may have specific functions that may help you better.

    https://pods.io/docs/code

    Hi, Pods he speaks http://pods.io/docs/code/pods/field/, but I don’t know. Thanks.

    You’re using a simple custom defined relationship, that field is configured so it’s a Label|Value. I don’t believe it’s stored that way in get_post_meta so I don’t quite follow what you’re attempting to do here.

    In Pods Vernacular, you need to use field() or display() for those values.

    This is discussed in this document on our website ‘Get Values from a Relationship Field’
    https://pods.io/tutorials/get-values-from-a-custom-relationship-field/

    Hmm, wait…

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘while() and newline (\n), how does it make sense?’ is closed to new replies.