What function this PHP code do? - php

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>

Related

compact and beautiful text coding [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
How to make my code more compact and beautiful. I think that it is possible to shorten it more, but unfortunately I do not know at all how. I hope that you will help me, dear specialists.
<?php
$postidid = get_the_ID();
$args = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => '-47,-56',
'post__not_in' => array( $postidid),
'meta_query' => array( array('key' => 'expost', 'compare' => 'NOT EXISTS'))
);
$args1 = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => '76',
'post__not_in' => array( $postidid),
'meta_query' => array( array('key' => 'expost', 'compare' => 'NOT EXISTS'))
);
$args2 = array(
'orderby' => 'rand',
'showposts' => 10,
'cat' => '34',
'post__not_in' => array( $postidid),
'meta_query' => array( array('key' => 'expost', 'compare' => 'NOT EXISTS'))
);
$news = new WP_query();
if( in_category(76)) {
$news->query($args1);
} elseif (in_category(34)){
$news->query($args2);
} else {
$news->query($args);
}
?>
<ul class="list" style="text-align:center;"><?php while ($news->have_posts()) : $news->the_post(); ?>
<li><img src="<?php echo get_post_image();?>" class="thumbnail" alt="<?php the_title_attribute(); ?>"><br><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
Sorry for My English
You can try this way. It can help you.
<?php
$postidid = get_the_ID();
$args = array(
'orderby' => 'rand',
'posts_per_page' => 10,
'post__not_in' => array( $postidid ),
'meta_query' => array(
array(
'key' => 'expost',
'compare' => 'NOT EXISTS'
)
),
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( - 47, - 56 )
)
)
);
$news = new WP_Query();
if ( in_category( 76 ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 76 )
)
);
$news->query( $args );
} elseif ( in_category( 34 ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 34 )
)
);
$news->query( $args );
} else {
$news->query( $args );
}
if ( $news->have_posts() ):?>
<ul class="list" style="text-align:center;">
<?php while ( $news->have_posts() ):
$news->the_post(); ?>
<img src="<?php echo get_post_image(); ?>" class="thumbnail" alt="<?php the_title_attribute(); ?>"><br><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
endif;

WP query and multiple key

I m trying to implement a multiple meta key filter in my wordpress. That's simple I get value to a form to filter my post. If I implement this with only "price" query wordked perfectly. If I add "genre" nothing work, query not working.
For field "genre" I m using checkbox from Advanced Custom Fields with this structure "homme : Homme / femme : Femme".
I test different thing like delete "price" and query on "genre" not working too...
I get value from this
<?php
if($_GET['minprice'] && !empty($_GET['minprice']))
{
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if($_GET['maxprice'] && !empty($_GET['maxprice']))
{
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 1000;
}
if($_GET['genre'] && !empty($_GET['genre']))
{
$genre = $_GET['genre'];
}
?>
my query looks like this
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
My loop with my query
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
// create a new instance of WP_Query
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
get_template_part( 'content-category', get_post_format() );
?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="clearfix"></div>
<?php bootstrap_pagination();?>
<?php } ?>
<?php else: ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
</div>
<?php wp_reset_query(); ?>
I tested this and it's work !
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
)
);
But that don't work
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
)
)
);
Please, can you help me beacause I m loosing my mind....
Thanks !
I think you are missing an wrapping array in the meta_query
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 28,
'paged' => $paged,
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array( $minprice, $maxprice ),
'compare' => 'BETWEEN'
),
array(
'key' => 'genre',
'value' => $genre,
'compare' => 'LIKE'
),
),
),
);

Show posts from query randomly

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!

Change get_posts to loop.php suitable version

I didn't know that get_posts doesn't work with loop.php. I already have this great loop.php file and I want to use it.
I created this code:
$pageposts = get_posts(
array(
'relation' => 'AND',
'post__in' => $postid,
'post_type' => 'event',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'st_date',
'value' => array($todate_s, $frmdate_s),
'compare'=> 'BETWEEN',
'type' => 'DATE'
),
),
)
);
if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?>
<?php else : ?>//etc
How I can convert the get_posts(//etc) to something that work with have_posts() and get_template_part('loop') at the end of my code?
Is this possible?
You get confused between query_posts(); and get_posts(); try this:
<?php
$pageposts = get_posts(
array(
'relation' => 'AND',
'post__in' => $postid,
'post_type' => 'event',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'st_date',
'value' => array($todate_s, $frmdate_s),
'compare'=> 'BETWEEN',
'type' => 'DATE'
),
),
)
);
foreach ( $pageposts as $post ) : setup_postdata( $post );
get_template_part('loop');
endforeach;
wp_reset_postdata();
?>
http://codex.wordpress.org/Template_Tags/get_posts
<?php
query_posts(
array(
'relation' => 'AND',
'post__in' => $postid,
'post_type' => 'event',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'st_date',
'value' => array($todate_s, $frmdate_s),
'compare'=> 'BETWEEN',
'type' => 'DATE'
),
),
)
);
get_template_part('loop');
wp_reset_query();
?>
http://codex.wordpress.org/Function_Reference/query_posts

WordPress Advanced Custom Fields - Meta Query no result

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 ?>

Categories