3

I have this query -

<?php if( is_page_template('taxonomy-listing_area-channel.php') ) { ?>
<?php
            $posts = get_posts(array(

    'post_type'     => 'adverts',
    'numberposts'   => 1,
    'order'         => 'random',

    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'associate_adverts',
            'value'     => '204',
            'compare'   => 'LIKE',
        )
    ),
));

        ?>

<?php //if it's returning the object

foreach($posts as $advert){

 $img = get_field("top_advert", $advert->ID);?>

 <img src="<?php echo $img["url"]; ?>"/>

<?php }?>

But for somr reaosn the posts are just showing as the last one entered and now randomly, I've never had this problem before but I have no idea where I'm going wrong, and help would be much appreciated!

Lucy Brown
  • 264
  • 4
  • 17

3 Answers3

1

Change here, You have syntax error, use single quotes inside double quotes,

<img src="<?php echo $img['url']; ?>"/>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
0

You need to change this

'post_type'     => 'adverts',
'numberposts'   => 1,
'order'         => 'random',

To

'post_type'        => 'adverts',
'posts_per_page'   => 1,
'orderby'          => 'rand',

Now you code will look like

<?php if( is_page_template('taxonomy-listing_area-channel.php') ) { ?>
<?php
            $posts = get_posts(array(

 'post_type'        => 'adverts',
 'posts_per_page'   => 1,
 'orderby'          => 'rand',

    'meta_query'    => array(
        'relation'      => 'AND',
        array(
            'key'       => 'associate_adverts',
            'value'     => '204',
            'compare'   => 'LIKE',
        )
    ),
));

        ?>

<?php //if it's returning the object

foreach($posts as $advert){

 $img = get_field("top_advert", $advert->ID);?>

 <img src="<?php echo $img["url"]; ?>"/>

<?php } }?>

Also you forgot to close you if statement.

hemnath mouli
  • 2,617
  • 2
  • 17
  • 35
0

Thanks for all the help,

It turns out it was a box that needed to be click on WPEngine to allow the random function in a query!

Lucy Brown
  • 264
  • 4
  • 17