Support

Account

Home Forums Add-ons Repeater Field Orderby title instead of foreach

Solved

Orderby title instead of foreach

  • Hi there, I need to ordere my subfield by subfield value instead of “drag and drop” order. I saw you tutorial but it’s for field foreach, not for subfield with a relationship. This is my code and I need to order post by related-post-name:

    <?php if(get_field('schede_prodotti')): ?>
    <table class="product-list simple-list">
    <?php while(has_sub_field('schede_prodotti')): ?>
    							
    <tr>
    	<td class="title">
    	<?php $posts = get_sub_field('relazione_prodotti');
    
    	if( $posts ): ?>
    		<?php foreach( $posts as $post): ?>
    			<?php setup_postdata($post); ?>
    			<span><?php the_title(); ?></span>
    		<?php endforeach; ?>
    		<?php wp_reset_postdata();?>
    	<?php endif; ?>
    	</td>
    	<td><a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a></td>
    	<td><a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a></td>
    </tr>
    
    <?php endwhile; ?>
    </table>
    <?php endif; ?>	

    Could you please help me?

  • Hi @gleenk it looks like you’ll have to retrieve the WP_Post IDs first and next perform an WP_Query or get_posts to order this stuff properly.

    Have a look at the following post “Order Post Objects by menu_order”. The Post Object and the Relationship field both return an array of WP_Post objects, so this solution should be possible for you to use.

    In your $args you’ll use the following to order by title 'orderby' => 'title'

    Please let me know if this works for you…

  • I don’t know how to apply your code to my more complex situation, could you please give me better help? 🙁

  • Hi @gleenk, lucky you I have a lazy Sunday and therefore I had some time to check out your code.

    First of all, please change the “Return Value” on your “Relationship” field from “Post Objects” to “Post IDs”, this way we get a nice array of all the WP_Post->IDs instead of the full WP_Post objects. See the screenshot attached for more info.

    So this brings me to the code:

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php while( has_sub_field( 'schede_prodotti' ) ): ?>
        <tr>
            <td class="title">
            <?php
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @see Screenshot: Change return value from Post Objects to Post IDs
                 * @var array
                 */
                $ids = get_sub_field( 'relazione_prodotti' );
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,    // Use the ids from above
                    'order'     => 'ASC',   // Order
                    'orderby'   => 'title'  // Field to order the posts by
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) :
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    
                        <span><?php the_title(); ?></span><?php
    
                    endforeach;
                    wp_reset_postdata();
    
                endif; // ( $posts )
            ?>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
            </td>
        </tr>
    
        <?php endwhile; // has_sub_field( 'schede_prodotti' ) ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    For better readability, I uploaded the code as a Gist.

    Please let me know if this solves your problem.

  • Hi, thanks a lot,
    I tried your code but it doesn’t print the_title().

  • Did you change the “Return Value” and resave the post?

  • Hi, I have no “Return Value” to set into Relationship field, look at my backend screenshot:

    http://postimg.org/image/gvlkugs3l/

  • Hi @gleenk, I just checked again, it seems you might need to update ACF and the relating plugins, as the fields I’m seeing for the “Relationship” Field are a bit different. See the screenshot attached, there you’ll spot the “Return Value” attribute.

  • I’ve got the last version. I see the field like you if not into a repeater. Try to create a repeater field and you see these disappear 🙂 Maybe it’s only a bug to fix 😀

  • Well I also have the “Return Value” fields when the “Relationship” field is within a “Repeater” that is really odd. Have a look at the screenshot attached.

    Could you please export your field groups as an XML file and attach them here? So I could have a look and maybe figure out what’s going on.

  • Here you are!

  • Well it looks like something is wrong or not updated on your side @gleenk. Because when I import your file, I get the “Return Value” attribute in the repeater as well. Could you please check if ACF and the Repeater Plugin are updated and maybe that you use the latest WordPress version too.

    Have a look at the screenshot and you’ll see your fields with the “Return Value” thing to be selected.

  • Yea! I updated WP to 3.6.1 (from 3.6) and options page and limiter field and now it has appeared. I’ll do a try with your code now…

  • Cool! Good luck and let me know wether the code above can be used as a solution.

  • Nothing to do, even selecting Post ID i can’t se the_title

  • Maybe you’ll have to re-save the relationships on the pages you are using this. So the database is updated.

  • It doesn’t work, even after updating the page 🙁 If you want to have a deep check i’ll give you credentials (by mail)

  • Ok, just checked out the code on a demo site I created with your XML file. I’m not sure, but we might forgot to tell WordPress about your post_type => ‘prodotti’ in the query. Could you please check if the following code works for you?

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php while( has_sub_field( 'schede_prodotti' ) ): ?>
        <tr>
            <td class="title">
            <?php
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @see Screenshot: Change return value from Post Objects to Post IDs
                 * @var array
                 */
                $ids = get_sub_field( 'relazione_prodotti' );
                // var_dump( $ids ); //uncomment this line to see the $ids getting loadded
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,    // Use the ids from above
                    'order'     => 'ASC',   // Order
                    'orderby'   => 'title',  // Field to order the posts by
                    'post_type' => 'prodotti' // We might forgot about your post type...
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) :
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    
                        <span><?php the_title(); ?></span><?php
    
                    endforeach;
                    wp_reset_postdata();
    
                endif; // ( $posts )
            ?>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
            </td>
        </tr>
    
        <?php endwhile; // has_sub_field( 'schede_prodotti' ) ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    You’ll find that I updated two lines:
    https://gist.github.com/neverything/6573468#file-acf-php-L17 and the more important one: https://gist.github.com/neverything/6573468#file-acf-php-L32

    Please let me know if this works for you and if not, please tell me so I’ll gladly have a look at your WordPress installation. Attached you find a screenshot of the working example table I rendered on my demo site.

  • I tried again but doesn’t work. It show title now but not in alphabetic order, it follows the normale darg&drop order.

  • Ahh I see the problem now. Instead of having multiple Relationships within one Repeater field, you created a repeater that has a
    – Relationship field to one product
    – A file
    – Another file

    Therefore we get the $ids for each Repeater field which will only return one as we only have one Relationship at the time.

    What the code tries to do is to order the $ids within each repeater, but there is only one. I’ll look at a different solution later today.

  • Ok I think I figured out a solution for you to test. First we need to loop through the while( has_sub_field( 'schede_prodotti' ) ) and prepare the data for furhter use in $ids and $files. Next we use these arrays containing the $post->IDs from the relationship field and the $files from the other fields in the repeater and prepare our data to get out.

    Here is the code:

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php
    
            $ids   = array(); // Empty array for the $post->IDs from the relationship fields
            $files = array(); // Empty array for the files from the repeater fields
    
            while( has_sub_field( 'schede_prodotti' ) ):
    
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @var array
                 */
                $id    = get_sub_field( 'relazione_prodotti' );
                $ids[] = $id[0]; // We need the first array of the return value of the relationship field
    
                /**
                 * Get the files
                 *
                 * Store the files under the $post->ID from the relationship field
                 * and use the field name for reference.
                 *
                 * @var $array
                 */
                $files[$id[0]]['scheda_tecnica'] = get_sub_field('scheda_tecnica');
                $files[$id[0]]['scheda_di_sicurezza'] = get_sub_field('scheda_di_sicurezza');
    
            endwhile; // has_sub_field( 'schede_prodotti' )
    
            if ( $ids ) : // Check if we have $ids from the posts.
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,        // Use the ids from above
                    'order'     => 'ASC',       // Order
                    'orderby'   => 'title',     // Field to order the posts by
                    'post_type' => 'prodotti'   // The custom post type you use
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) : // Check if we have $posts
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
                        <tr>
                            <td class="title">
                                <span><?php the_title(); ?></span>
                            </td>
                            <td>
                                <a href="<?php echo $files[$post->ID]['scheda_tecnica']; ?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
                            </td>
                            <td>
                                <a href="<?php echo $files[$post->ID]['scheda_di_sicurezza']; ?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
                            </td>
                        </tr><?php
    
                    endforeach; // ( $posts as $post )
                    wp_reset_postdata();
    
                endif; // ( $posts )
            endif; // ( $ids )
        ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    And again I updated the Gist for better readability: https://gist.github.com/neverything/6573468

    Let me know if this works now with the sorting of the titles.

  • Yeah! It works! I had to put ‘posts_per_page’ to ‘-1’ and it’s perfect! Why wouldn’t you add this feature (to choose different order from drag and drop) into the plugin? Is it possible? 🙂

  • Happy that it works now, that’s something you would have to ask Elliot. I’m just a coder how loves this plugin and therefore I try to help out in the forum ;-).

    Here is the link to the Feature Request page: http://support.advancedcustomfields.com/forums/forum/feature-requests/

  • Did it! I hope Elliot will consider this, unfortunately I can’t tag him directly here: http://support.advancedcustomfields.com/forums/topic/possibility-to-choose-different-order-form-dragdrop/
    Thanks again guy!

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

The topic ‘Orderby title instead of foreach’ is closed to new replies.