Support

Account

Home Forums Front-end Issues Display all posts with special treatment for relationships

Solving

Display all posts with special treatment for relationships

  • Hello, I am slowly learning my ACF. Today, I’m trying to display a list of custom posts “strategies” on different “value” posts.

    Some values have a green color scheme, some have a yellow color scheme. Among the full list of strategies displayed on the value page, those that are related to the value are colored with a class … and even have a special colored image. Otherwise, they’re just gray.

    Value X Post Page (Scheme = Green)

    strategy 1 (unrelated –> gray text, gray s1 icon)
    strategy 2 (related –> green text, green s2 icon)
    strategy 3 (related –> green text, green s3 icon)

    Value Y Post Page (Scheme = Blue)

    strategy 1 (related–> blue text, blue s1 icon)
    strategy 2 (related –> blue text, blue s2 icon)
    strategy 3 (related –> blue text, blue s3 icon)

    Value Z Post Page (Scheme = Yellow)

    strategy 1 (unrelated –> gray text, gray s1 icon)
    strategy 2 (unrelated –> gray text, gray s2 icon)
    strategy 3 (related –> yellow text, yellow s3 icon)

    If anyone has any ideas on how to achieve this, I’ll be grateful. The first thing I tried was a relationship field on the value post. I wasn’t sure, however, how to pull unrelated strategies, mix them into the list and order the strategies correctly — the related and unrelated.

    The second difficult part was how to display the correct images. Since there are three types of values, I have three different images for each strategy.

  • I’m going to try to improve and simplify my question to see if that helps anyone answer.

    1. I have a list of all the $values that impact a $strategy. I got that from the given strategy via repeater field with a post_object select. Cool!

    2. I’m trying to get a list of all the other $values. I want the $values not selected in that repeater field.

    I’m not sure how to get things that aren’t selected. Do I try to “subtract” things that are selected from the full list? Hopefully my comments below help explain.

                        <?php 
    // Query to get $values this $strategy does NOT impact
    // strategy_impact = repeater
    // impact_value_select = post_object
    // custom_value_description = text not used here, but elsewhere           
                        if ( get_field('strategy_impact') ): ?>
                        <div class="col-md-4">
                            <h4>No Impact</h4>
                        <?php while(has_sub_field('strategy_impact')) : ?>
                            <?php 
                            $impact_values = get_sub_field('impact_value_select');
                            $impact_values->post_title;
                            $impact_IDs = array($impact_values->ID);
                            $impact_IDs_comma = implode(",", $impact_IDs);
    //                      $custom_value_description = get_sub_field('custom_value_description');
    
                            // echo $impact_IDs_comma; 
                            // above results in a list of numbers 229 233 150 239 134 221 223 
                            // I thought it would give me commas like 229,233,150...
    
                            echo$impact_values->ID.',';
                            // above gives me 229, 233, 150, 239, 134, 221, 223,
                            // in order to get "a list of all UNselected non-post_object posts of this post_type" I thought I could use that as a 'post__not_in' parameter in a WP_Query, but it's not working 
    
                            ?>
    <?php /*                        $args = array (
                                'numberposts'   => '-1',
                                'orderby'       => 'title',
                                'order'         => 'ASC',
                                'post_type'     => 'values',
                                'post__not_in'  => $impact_IDs_comma
                                );
                            $all_values = new WP_Query( $args);
                            if ( $the_query->have_posts() ) : 
                            while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                            <h2><?php the_title(); ?></h2>
    <?php endwhile; ?><?php endif; ?>
    */?>
                            <?php endwhile; ?>
                         </div>
                        <?php endif; ?>
  • I tried this, but no luck. There are 12 $values.

    $noImpact = new WP_Query( 
                                array(
                                    'numberposts'  => 12,
                                    'post_type'    => 'values',
                                    'post__not_in' => array($impact_IDs_comma)
                                ) 
                            );
                            while ($noImpact->have_posts()) : $noImpact->the_post(); ?>
                            <h4><strong><a href="<?php echo get_permalink(); ?>" class="text-disabled">
                            <i class="fui-check-inverted text-disabled"></i> 
                            <?php the_title(); ?></a></strong></h4> 

    My list of $values post_objects of $strategies is 7 items long. The list that should only contain unrelated $values is 10 items long, which I don’t understand. I thought it would either be 5 items (good) or 12 (bad) but not 10 (weird).

  • I believe posts__not_in accepts an array not a string. You have an array with a single string it in it. It needs to be like:

    
    'post__not_in' => array(1,2,4,5)
    

    Remove your implode and see if that does the trick.

  • Thanks for the reply. I tried this:

    'post__not_in' => array($impact_IDs)'

    Where

    $impact_values = get_sub_field('impact_value_select');
    $impact_IDs = array($impact_values->ID.',');

    So, I’m not using implode, but that only knocks of the first post_object with the first ID.

  • Hi @rktlwm

    The ‘post__not_in’ param expects an array or ID’s, not a string, nor an object.

    Can you please confirm that the code:

    
    $impact_values = get_sub_field('impact_value_select');
    
    echo '<pre>';
    	print_r( $impact_values );
    echo '</pre>';
    die;
    

    Displays only an array of ID’s? Can you please post the result here?

    Thanks
    E

  • Hi @elliot

    Yes, I’m trying to get an array, but can’t figure out how. When I print_r, as you requested, I get one sad ID.

    WP_Post Object
    (
        [ID] => 229
        [post_author] => 3
        [post_date] => 2014-01-17 04:25:49
        [post_date_gmt] => 2014-01-17 09:25:49
        [post_content] => 
        [post_title] => Usability
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => usability
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2014-01-20 09:46:18
        [post_modified_gmt] => 2014-01-20 14:46:18
        [post_content_filtered] => 
        [post_parent] => 0
        [menu_order] => 0
        [post_type] => values
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    I’ve been trying to get 229, 233, 150, 239, 134, 221, 223, because I was able to echo it once…

                        
    <?php 
    // Values this strategy does NOT impact
    // strategy_impact = repeater
    // impact_value_select = post_object
    // custom_value_description = text not used here, but elsewhere           
                        if ( get_field('strategy_impact') ): ?>
                            <?php while(has_sub_field('strategy_impact')) : ?>
                            <?php 
                                $impact_values = get_sub_field('impact_value_select'); 
                                $impact_IDs = array($impact_values->ID);
                                $impact_IDs_comma = implode(",", $impact_IDs); ?>
                            <?php $the_thing = $impact_values->ID.','; ?>
                            <?php
                            echo $the_thing
                            ?>
                        <?php endwhile; ?>  
    

    I get 229, 233, 150, 239, 134, 221, 223,

    I wish I could get that into the “post__not_in” parameter, but after the endwhile …

    Sorry if I’m going the taking the long route in the wrong direction.

    What I’m really trying to accomplish is to get a full list of the “values” post_type but change the class/appearance of the posts based on whether or not they are post_objects of the current page, as determined by the strategy_impact sub-field.

    Even if what I’m trying here works, it’s not the ideal way to do it because it generates two lists instead of one — one that’s all the objects, one that’s not all the objects — instead of one list where objects are given different classes than the others.

  • Hi @rktlwm

    I have simplified your code down to only 1 idea, this should help you understand how to get the selected $post (sub field), and then append it’s ID to an array. After the repeater field loop is complete, you have an array containing all the IDs.

    I’m not sure if this is what you are after, but your code so far hasn’t been easy to understand. I think a simpler example may help communicate the issue / solution:

    
    <?php 
    
    $impact_IDs = array();
    
    if( have_rows('strategy_impact') )
    {
    	while( have_rows('strategy_impact') )
    	{
    		the_row();
    		
    		$selected_post = get_sub_field('impact_value_select');
    		
    		$impact_IDs[] = $selected_post;
    	}
    }
    
    echo '<pre>';
    	print_r( $impact_IDs );
    echo '</pre>';
    die;
    
    ?>
    
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Display all posts with special treatment for relationships’ is closed to new replies.