0

I'm trying to display 4 random instances of a custom field from random posts without repeating the custom field. In other words, it's okay to get several custom field instances from one post, but not okay to get the same custom field twice instance.

So I started with one query that produced 4 unique random instances of the custom field. See this StackOverflow Question

This worked fine except for the fact that all 4 instances were from the same post, so I tried 4 queries storing the value of the custom field in an array and tried checking each query against the array, but it doesn't seem to be working. I must be missing something. Here is my code showing all four queries:

        <?php $args = array( 
                'orderby'        => 'rand',
                'ignore_custom_sort' => TRUE,
                'post_type' => 'bios', 
                'tax_query' => array(
                    array(
                        'taxonomy' => 'bio_type',
                        'field' => 'slug',
                        'terms' => 'leadership-team',
                    ),
                ),
                'posts_per_page' => 1, 
                'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
                'meta_value' => '', // check that meta_key is not empty
                'meta_compare' => '!='

             );  ?>            
    <?php

    // The Query to show a specific Custom Field
    $the_query = new WP_Query($args);
    $ids = array();
    ?>
    <?php 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
    ?>


    <?php 

    // The Loop
    echo '<div class="row vertical-pad-small">';
    echo '<div class="columns large-8">';

     $acc = simple_fields_fieldgroup('bio_accordion');

          $keys = array_rand($acc);
          $title = $acc[$keys]['title'];  
            echo '<div>';
                echo '<a>'.$title.'</a>';
                echo '<span>';
                echo $acc[$keys]['content'];
                echo '</span>';

                echo '</div>';
    echo '</div>';
    echo '</div>';
    endwhile;
    // Reset Post Data
    wp_reset_postdata(); 
    $ids[] = $title;
    ?>

    <pre>
        <?php var_dump($ids); ?>
    </pre>

        <?php

            $args2 = array( 
                'orderby'        => 'rand',
                'ignore_custom_sort' => TRUE,
                'post_type' => 'bios', 
                'tax_query' => array(
                    array(
                        'taxonomy' => 'bio_type',
                        'field' => 'slug',
                        'terms' => 'leadership-team',
                    ),
                ),
                'posts_per_page' => 1, 
                'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
                'meta_value' => '',$ids,  // check that meta_key is not empty and is not in $ids
                'meta_compare' => '!='

             );  
        ?> 

    <?php   $the_query2 = new WP_Query($args2);
    while ( $the_query2->have_posts() ) : $the_query2->the_post(); 
            ?>
    <?php   
    $bio_fields = simple_fields_fieldgroup('bio');
    $adult_image = $bio_fields['adult_picture']['image']['medium_square'];
    $title = $bio_fields['title'];
    $name = get_the_title();
    ?>
    <?php 

    // The Loop
    echo '<div class="row vertical-pad-small">';
    echo '<div class="columns large-8">';


     $acc = simple_fields_fieldgroup('bio_accordion');

          $keys = array_rand($acc);
          $title = $acc[$keys]['title'];
                echo '<div>';
                echo '<a>'.$title.'</a>';
                echo '<span>';
                echo $acc[$keys]['content'];
                echo '</span>';
                echo '</div>';
    echo '</div>';
    echo '</div>';
                    endwhile;
   // Reset Post Data
    wp_reset_postdata(); 
    $ids[] = $title; 
    ?>
     <?php
        $args3 = array( 
                'orderby'        => 'rand',
                'ignore_custom_sort' => TRUE,
                'post_type' => 'bios', 
                'tax_query' => array(
                    array(
                        'taxonomy' => 'bio_type',
                        'field' => 'slug',
                        'terms' => 'leadership-team',
                    ),
                ),
                'posts_per_page' => 1, 
                'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
                'meta_value' => '',$ids,// check that meta_key is not empty and is not in $ids
                'meta_compare' => '!='

             );  
    ?> 

    <?php   $the_query3 = new WP_Query($args3);
    while ( $the_query3->have_posts() ) : $the_query3->the_post(); 
    ?>

    <?php 

    // The Loop
    echo '<div class="row vertical-pad-small">';
    echo '<div class="columns large-8">';

    $acc = simple_fields_fieldgroup('bio_accordion');

          $keys = array_rand($acc);
           $title = $acc[$keys]['title'];
                echo '<div>';
                echo '<a>'.$title.'</a>';
                echo '<span>';
                echo $acc[$keys]['content'];
                echo '</span>';
                echo '</div>';
    echo '</div>';
    echo '</div>';
                    endwhile;
   // Reset Post Data
    wp_reset_postdata(); 
    $ids[] = $title; 
    ?>

     <?php
            $args4 = array( 
                'orderby'        => 'rand',
                'ignore_custom_sort' => TRUE,
                'post_type' => 'bios', 
                'tax_query' => array(
                    array(
                        'taxonomy' => 'bio_type',
                        'field' => 'slug',
                        'terms' => 'leadership-team',
                    ),
                ),
                'posts_per_page' => 1, 
                'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
                'meta_value' => '',$ids, // check that meta_key is not empty and is not in $ids
                'meta_compare' => '!='
            );  
    ?> 
   <?php  $the_query4 = new WP_Query($args4);
    while ( $the_query4->have_posts() ) : $the_query4 ->the_post(); 
    ?>
     <?php 

    // The Loop
    echo '<div class="row vertical-pad-small">';
    echo '<div class="columns large-8">';


     $acc = simple_fields_fieldgroup('bio_accordion');


          $keys = array_rand($acc);
          $title = $acc[$keys]['title'];
                echo '<div>';
                echo '<a>'.$title.'</a>';
                echo '<span>';
                echo $acc[$keys]['content'];
                echo '</span>';
                echo '</div>';
    echo '</div>';
    echo '</div>';
    endwhile;
   // Reset Post Data
    wp_reset_postdata();
    $ids[] = $title; 
    ?>

Can anyone help me?

  • You have this error in your args, remove the '', 'meta_value' => '',$ids, – Augwa Mar 9 '17 at 21:49
  • You mean change it to 'meta_value'=>$ids, ? If I do that I get this error: Warning: trim() expects parameter 1 to be string, array given – Diah Mar 9 '17 at 21:54
  • That means it's expecting a string. maybe you need to do implode(',', $ids). Sorry not a wordpress developer so I don't really know what this does other than that's an obvious error. – Augwa Mar 9 '17 at 21:55
0

I found a solution, not quite what I wanted but it got the desired effect. Since I only had two posts with the custom fields populated I initiated a query that produced the custom field I wanted and stored the the value of the custom field in a variable and then initiated another query and checked the custom field against that variable.

Here is the code:

    <?php

$do_not_duplicate = array();
$args = array(
    'orderby' => 'rand',
    'ignore_custom_sort' => TRUE,
    'post_type' => 'bios',
    'tax_query' => array(
        array(
            'taxonomy' => 'bio_type',
            'field' => 'slug',
            'terms' => 'leadership-team',
        ),
    ),
    'sf_meta_key' => 'bio_accordion/title',
    'meta_value' => '',
    'compare' => '!=',
    'posts_per_page' => 1,
);
?> 
 <?php


$the_query = new WP_Query( $args );
?>
<div id="get-connected" class="vertical-pad lazy" data-original="<?php echo get_stylesheet_directory_uri(); ?>/dist/img/get_connected_back.png">
    <div class="row">

                <?php 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
        $acc = simple_fields_fieldgroup('bio_accordion');
        $i = 0;
        $len = count($acc);
        $keys = array_rand($acc, min(2, sizeof($acc)));
        foreach($keys as $key) { 
            if ($i == 0) {

?>  
        <div class="columns medium-order-2 small-12 medium-5">
            <h3 class="vertical-pad text-center wow slide-in">Featured Articles</h3>
            <div class="give gc-bucket wow slide-in lazy">

                <div class="gc-bucket-content">
                    <p>
                        <?php echo $acc[$key]['content']; ?>
                    </p>
                </div>
            </div>
        </div>
        <?php } else if($i == 1) { ?>
        <div class="columns medium-order-1 small-12 medium-7">
            <div class="row">
                <div class="columns small-12">
                    <div class="download gc-bucket wow slide-in lazy">
                        <div class="gc-bucket-content">

                            <p>
                                <?php echo $acc[$key]['content']; ?>
                            </p>

                        </div>
                    </div>
                </div>
            </div>


            <?php 
                            }$i++;}
        $do_not_duplicate[] = $post->ID;
        endwhile;
        wp_reset_postdata(); 
        ?>
    <div class="row align-right">

            <?php
            $args = array(
                'orderby' => 'rand',
                'ignore_custom_sort' => TRUE,
                'post_type' => 'bios',
                'post__not_in' => $do_not_duplicate,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'bio_type',
                        'field' => 'slug',
                        'terms' => 'leadership-team',
                    ),
                ),
                'sf_meta_key' => 'bio_accordion/title',
                'meta_value' => '',
                'compare' => '!=',
                'posts_per_page' => 2,
            );
            $the_query = new WP_Query( $args );
            while ( $the_query->have_posts() ): $the_query->the_post();
            $acc = simple_fields_fieldgroup( 'bio_accordion' );
            $keys = array_rand( $acc );
            $title = $acc[ $keys ][ 'title' ];
            $content = $acc[ $keys ][ 'content' ];

            ?>
                <div class="columns small-12 medium-12 large-8">

                    <div class="blog gc-bucket wow slide-in lazy">
                        <div class="gc-bucket-content">
                            <p>
                                <?php echo $content ; ?>
                            </p>

                        </div>
                    </div>
                </div>
            <?php
            endwhile;
            wp_reset_postdata();
            ?>

            </div>
        </div>
    </div>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.