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!
Change here, You have syntax error, use single quotes inside double quotes,
<img src="<?php echo $img['url']; ?>"/>
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.
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!
Related
I am working in my first PHP project and I don't understand what does this piece of code does. May anyone help me please?
I don't work very well with php.
<?php
$args = array(
'post_type' => 'manual-pdf',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
// ['relation'] => 'OR', // use this for a different comparison
array(
'key' => 'wpcf-category',
'value' => 2,
'compare' => '='
),
array(
'key' => 'wpcf-version',
'value' => 1,
'compare' => '='
)
));
query_posts($args);
if ( have_posts() ) :
while (have_posts()): the_post();
?>
<a target="_blank" href="<?=$url = types_render_field("url", array('raw' => 'false'));?>"><? the_title(); ?></a><br><?=$descricao = types_render_field("description", array('raw' => 'false'));?><hr>
<?
endwhile;
else :
endif;
wp_reset_query();
?>
Basically, this part made me confused mostly:
<a target="_blank" href="<?=$url = types_render_field("url", array('raw' => 'false'));?>"><? the_title(); ?></a><br><?=$descricao = types_render_field("description", array('raw' => 'false'));?><hr>
I have this query -
<?php if (has_term( 'channel', 'listing_area' )) { ?>
<?php
$posts = get_posts(array(
'numberposts' => 3,
'post_type' => 'adverts',
'order_by' => 'title',
'order' => 'random',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'associate_adverts',
'value' => '1822',
'compare' => 'LIKE',
)
),
));
?>
<?php //if it's returning the object
foreach($posts as $advert){
$img = get_field("advert", $advert->id);?>
<img src="<?php echo $img["url"]; ?>"/>
<?php }?>
I need to show an image called from an advanced custom field called 'advert' which is set to use the image array option where I'm calling the post title.
How can I do this? Thanks.
Read the get_field doc
In your loop, use $img = get_field("advert", $advert->ID), return an array.
Then to get the url, use $img["url"]
The doc
Question for all of ya. I have the current query running on my search page template, and it seems to be working fine if my search query seems to be included in the title of the post, but when I include a meta query to try and also look in another spot for the search term, it doesn't gather any results, only the same results it had before.
Second question, for some reason it still is only displaying 6 (the number of posts set in WP Admin) posts, and not listening to the query.
<?php // WP_User_Query arguments
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => '*'.$search_term.'*',
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$wp_course_query = new WP_Query($args);
// Get the results
$courses = $wp_course_query; ?>
<?php // Check for results
if (!empty($courses->get_posts())) { ?>
<ul class="course-list">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li> <?php the_title(); ?> </li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
<?php } else { ?>
<p>No courses match that query</p>
<?php } ?>
Things I've tried:
Hard coding the value, nothing there.
Removing * from 's'
It seems that this is just impossible in WordPress, so I had to do this another way.
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => $search_term
);
$args2 = array (
'post_type' => 'courses',
'posts_per_page' => -1,
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$courses1 = get_posts($args);
$courses2 = get_posts($args2);
$merged = array_merge($courses1, $courses2);
$post_ids = array();
foreach ($merged as $item) {
$post_ids[] = $item->ID;
}
$unique = array_unique($post_ids);
$posts = get_posts(array(
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $unique,
'posts_per_page' => -1
)); ?>
I am using WPML and ACF in my WP.
Now I wanna list posts from the category ID 399 with the ACF Field "organization_type" and the value key "socialbusiness" but they do not show up.
This are my query tries:
$args = array(
'post_type' => 'post',
'cat' => 399,
'posts_per_page' => -1,
'meta_query' => array(
//'relation' => 'OR',
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
//'compare' => '='
)
)
);
//unset($args);
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'cat' => 399,
'meta_key' => 'organization_type',
'meta_value' => 'socialbusiness'
);
// query
query_posts( $args );
while( have_posts() ) {
What am I doing wrong?
You should have just one variable $args because your first declaration of the variable is override by your second variable.
In your case your code should look like :
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 399
)
),
'meta_query' => array(
array(
'key' => 'organization_type',
'value' => 'socialbusiness',
'compare' => '=',
'type' => 'CHAR'
),
)
);
$items = new WP_Query($args);
?>
<?php if($items->have_posts()) : ?>
<div class='item'>
<?php while($items->have_posts()) : $items->the_post() ?>
.....
<?php endwhile ?>
</div>
<?php endif ?>
The following argument works properly for search results, however they aren't displaying in the proper ascending order. I'm looking to display the most expensive at the top, and so on.
Here's my code, where the $input_price is user defined by preset values within a form.
$args = array(
'post_type' => 'post',
's' => $query,
'post_status' => 'publish',
'cat' => "$category_name",
'meta_query' => array(
array(
'key' => 'price',
'value' => "$input_price",
'type' => 'numeric',
'compare' => '<=',
'order' => 'ASC'
)
)
);
$the_query = new WP_Query( $args );
?>
I don't think this is the problem, but the 'price' is being output with currency and comma values, as seen below.
<?php $meta = get_post_custom($post->ID);
echo '$'.number_format($meta['price'][0], 0, '.', ',').''; ?>
Annnd here's the loop to display
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php $meta = get_post_custom($post->ID);
echo '$'.number_format($meta['price'][0], 0, '.', ',').''; ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no products within those search terms! Try Adjusting the maximum price.</p>
Thanks!
try DESC instead of ASC for greatest to smallest?
Found the answer!
The code below works as intended. I re-ordered my array slightly to include 'orderby' at the very end, while also specifying which key to order by.
<?php
$args = array(
'post_type' => 'post',
's' => $query,
'post_status' => 'publish',
'cat' => "$category_name",
'meta_query' => array(
array(
'key' => 'price',
'value' => "$input_price",
'type' => 'numeric',
'orderby' => 'meta_value_num',
'compare' => '<=',
)
),
"meta_key"=>"price",
"orderby"=>"meta_value_num",
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>