0

I'm just hit a development brick wall and can't seem to find a solution to this problem.

I've running two WP_Query's the aim is one displays premium stockists and the other one displays the none premium stockists. I've added a tick-box that allow's me to use a conditional to check if its a premium stockist or not.

  1. How do I add the correct logic in *if the user is a premium member display the premium stockists. These must appear at the top of the page above the other stockists due to them been premium.

  2. I'm using a plugin called Advanced custom fields to generate the premium true/false field. website is enter link description here

Code I've got.

<?php

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => 5,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => 'store-category',
            'field' => 'id',
            'terms' => array($_POST['region'])
            )
        )
    );

$query = new WP_Query($args);

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

$mapurl = get_post_meta($post->ID, 'lj_mapurl', true); ?>

<div class="usercontent">
    <div class="stockist-content green-bg">
        <strong><?php the_title(); ?></strong>
        <?php the_content(); ?>
    </div>
    <div class="stockist-image">
        <?php   if ( has_post_thumbnail() ) {
            $postthumnail_count++;
            the_post_thumbnail('stock');
        }
        ?> 
    </div>
</div>
<div class="clear" style="clear:both;"></div>
<div class="clr">&nbsp;</div>
<a class="viewMap" href="<?php echo $mapurl; ?>" title="Find <?php the_title(); ?> on a map (opens in a new window)" target="_blank">View Map</a>
<div class="clr">&nbsp;</div>
<hr />
<?php endwhile; ?>
<?php else: ?>
    <p>Sorry, content not found</p>

<?php endif; ?>
<?php wp_reset_query(); ?>

<?php

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => 5,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => 'store-category',
            'field' => 'id',
            'terms' => array($_POST['region'])
            )
        )
    );

$query = new WP_Query($args);

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

$mapurl = get_post_meta($post->ID, 'lj_mapurl', true); 
$premium = get_field('premium_stock_supplier');
if ( ! $premium ) { // this is basically short for $premium == false
    ?>

    <div class="usercontent">
        <div class="stockist-content">
            <strong><?php the_title(); ?></strong>
            <?php the_content(); ?></div></div>


            <a class="viewMap" href="<?php echo $mapurl; ?>" title="Find <?php the_title(); ?> on a map (opens in a new window)" target="_blank">View Map</a>
            <div class="clr">&nbsp;</div>
            <hr />
            <?php  } ?>



        <?php endwhile; ?>
    <?php else: ?>
    <p>Sorry, content not found</p>

<?php endif; ?>

updated code for displaying premium stockists (doesn't work)

<?php
              $args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'meta_key' => 'premium',
            'meta_value' => '0',
            )
        ),
    'posts_per_page' => 5,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => 'store-category',
            'field' => 'id',
            'terms' => array($_POST['region'])
            )
        )
    );

code for displaying none premium stockists (doesn't work)

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'meta_key' => 'premium',
            'meta_value' => '0',
            )
        ),
    'posts_per_page' => 5,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => 'store-category',
            'field' => 'id',
            'terms' => array($_POST['region'])
            )
        )
    );
2
  • How do you identify premium users? Please pare that code down to only the part relevant to this question.
    – s_ha_dum
    Commented Apr 10, 2014 at 21:49
  • And can you indent correctly? The code is difficult to read
    – Tom J Nowell
    Commented Apr 10, 2014 at 21:56

1 Answer 1

0

Your queries are showing the same output, because they are identical

Here is your first queries arguments:

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => 5,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => 'store-category',
            'field' => 'id',
            'terms' => array($_POST['region'])
        )
    )
);

Here is your second queries arguments:

$args = array(
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => 5,
    'paged' => get_query_var('paged'),
    'tax_query' => array(
        array(
            'taxonomy' => 'store-category',
            'field' => 'id',
            'terms' => array($_POST['region'])
        )
    )
);

You ask for the same thing both times, you get the same thing both times.

Since you're using ACF you should query by the post meta for your ACF field:

e.g. something similar to this:

$args = array(
    'meta_key' => 'premium', // I guessed your field would be called premium, but this might not be the case
    'meta_value' => 'yes'
);

// get results
$the_query = new WP_Query( $args );

... etc..


$args = array(
    'meta_key' => 'premium',
    'meta_value' => 'no'
);

// get results
$the_query = new WP_Query( $args );

ACF fields are post meta/custom fields too, they're just wrapped behind a fancy UI/API

4
  • I'm aware of this Tom - maybe should of mentioned it - I'm looking to create a query that doesn't display the premium users.
    – Dannyw24
    Commented Apr 10, 2014 at 22:03
  • And it posted before I could finish answering, see edited answer
    – Tom J Nowell
    Commented Apr 10, 2014 at 22:04
  • Would the 'meta_value => 'no' be 'meta_value => 0' for false and '1' for true
    – Dannyw24
    Commented Apr 10, 2014 at 22:08
  • probably, like the ACF field name/key I cant see what you've chosen as your premium and non premium values. Try grabbing the field and passing it into print_r or var_dump to see what the respective values are to put in your query
    – Tom J Nowell
    Commented Apr 11, 2014 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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