Advanced custom field query in WordPress - wordpress

I am trying to list all the post while advanced custom field value ispremium=>yes and post issticky in my WordPress site. I have following code in my page. It is listing all the post but not checking ispremium=>yes and issticky post rather showing all the posts.
What's wrong in my code?
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes'
);
// query
$the_query = new WP_Query( $args and is_sticky());
?>
<?php if($the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('event_thumbnail'); ?>" />
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

Try this:
$args = array(
'posts_per_page' => -1,
'post__in' => get_option( 'sticky_posts' ),
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes',
'ignore_sticky_posts' => 1,
);
$the_query = new WP_Query( $args );

Related

Related post by custom field

I use wordpress acf custom post type to create my custop post type.
this code (in my custop post type page php) to display the related post of a taxonomy type, now i want to display by Custom Field instead of taxonomy. Thank you for helping me solve my problem
<?php
global $post;
$current_post_type = get_post_type( $post );
$args = array(
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'ID',
'post_type' => $current_post_type,
'post__not_in' => array( $post->ID )
);
$rel_query = new WP_Query( $args );
if( $rel_query->have_posts() ) :
?>
<h1 id="recent">Related</h1>
<div id="related" class="group">
<ul class="group">
<?php
// The Loop
while ( $rel_query->have_posts() ) :
$rel_query->the_post();
?>
<li>
<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
<article>
<h1 class="entry-title"><?php the_title() ?></h1>
<div class="name-date"><?php the_time('F j, Y'); ?></div>
<div class="theExcerpt"><?php the_excerpt(); ?></div>
</article>
</a>
</li>
<?php
endwhile;
?>
</ul><!-- .group -->
</div><!-- #related -->
<?php
endif;
// Reset the query
wp_reset_query();
?>
WP_Query Object accept arguments to query custom field values (more info here: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/). So you need the name of your custom field in this example 'location' and then the value you want 'Melbourne' (you will get the value using get_field('location')).
So your args should look like this:
$args = array(
'posts_per_page' => 3,
'order' => 'DESC',
'orderby' => 'ID',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
'post_type' => $current_post_type,
'post__not_in' => array( $post->ID )
);
Hope this helps

Alternatives to using get_pages in WordPress to get children page data

I am using get_pages to fetch some data from the children pages of a parent in WordPress (like a custom loop) - but it doesnt work when trying to fetch some data as set by the Advanced Custom Fields plugin for some strange reason... Is there an alternative / better way to acheive what I want? Code below works apart from fetching the ACF field called 'job_title'.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'sort_order' => 'DESC',
'sort_column" => "post_name',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$pages = get_pages($args); ?>
<div class="childrenFetchedLoopWrapper">
<?php foreach( $pages as $page ) { ?>
<div class="feedItemWrapper wpb_animate_when_almost_visible wpb_fadeInUp fadeInUp" style="background-image: url('<?php echo get_the_post_thumbnail_url($page->ID, 'full'); ?>')">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<img src="/wp-content/themes/salient-child/images/aspectTrans.png" alt="*" title="*" />
<h3><?php echo $page->post_title; ?></h3>
<p><?php the_field('job_title'); ?></p>
</a>
</div>
<?php } ?>
</div>
Replace <?php the_field('job_title'); ?> with <?php the_field('job_title', $page->ID); ?>.
OR
You can use WP_Query for alternative solution.
or you can also get acf value using get_post_meta();
Try Out this code. I hope it helps.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post();
$id = get_the_ID(); ?>
<p><?php the_field('job_title', $id); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>

Show posts from custom taxonomy

Update 2
Adding name as field instead of the slug and adding the_title() just give me an echo of the page title...
$args = array(
'post_type' => 'feestlocaties',
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'name',
'terms' => the_title(),
),
),
);
Update Jonnhyd23's code worked like a charm!! Thanks!
Is there a way you can make the terms dynamic? Like the title is Amsterdam can I do something like 'terms' => '<?php the_title(); ?>' or something like that?
I've been going at this for the last couple of hours. Maybe someone here can help me?
I want to show specif posts from a custom taxonomy in a loop.
This is the situation:
custom taxonomy: feestlocaties
And the the posts i want to show have Amsterdam selected (checked) (like categories).
Code i tried:
<div id="main-filter">
<!-- Start the Loop. -->
<?php $args = array(
'post_type' => 'feestlocaties',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'slug',
'terms' => 'amsterdam',
),
),
); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
<!-- Test if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box is given the CSS class "post". -->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="container post-item">
<div class="col-sm-3 no-padding">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php the_post_thumbnail(array(400,355)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
</div>
<div class="col-sm-9 no-padding">
<h1 class="overzicht"><?php the_title(); ?></h1>
<?php html5wp_excerpt('html5wp_index'); ?>
<div class="col-sm-12 no-padding loop-overzicht">
<?php $prijs = get_Field('vanaf_prijs'); ?>
<?php $pers = get_Field('aantal_personen'); ?>
<?php $time = get_Field('tijdsduur'); ?>
<ul class="loop-opsomming text-right">
<li><?php echo '<i class="fa fa-euro"></i>Vanaf ' . $prijs . ' p.p.' ?></li>
<li><?php echo '<i class="fa fa-group"></i>Vanaf ' . $pers . ' personen' ?></li>
<li><?php echo '<i class="fa fa-clock-o"></i>Vanaf ' . $time . ' uur' ?></li>
</ul>
</div>
</div>
</div>
</a>
<?php wp_pagenavi(); ?>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
But nothing is showing. Any help would be great. Thanks!
Are you just showing the arguments you're using for WP_Query, or is this all of your code? Try using the tax_query parameter.
$args = array(
'post_type' => 'your_post_type',
'tax_query' => array(
array(
'taxonomy' => 'feestlocaties',
'field' => 'slug',
'terms' => 'amsterdam',
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post();
//execute code
endwhile; endif; wp_reset_postdata();
So, I fiddeld around abit and this is the code that works for me. Cheers Johnnyd23
<?php $args = array(
'post_type' => 'feestlocaties',
'showposts' => '3',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'locatie',
'field' => 'name',
'terms' => get_the_title(),
),
),
); ?>
<?php $query = new WP_Query( $args ); ?>
<?php if( $query->have_posts() ): while( $query->have_posts() ): $query->the_post(); ?>
This will make the title the post dynamicly in the WP_Query.

Display post for a week

Can someone help me how to display the post only for a week? i have this code that works fine in post_date:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?
Thanks.
This Might help you to get solution:
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The previous answer was along the good track. Just modify your date_query:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>

Display two posts from each post type with wp_query

I have used the following code to display the one post from each post.
$post_types = array('a', 's','d','f','g');//post type names
foreach( $post_types as $post_type) :
// The Query
$the_query = new WP_Query( array( 'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="issue-content masonItem <?php echo $post_type; ?>">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id() );
$resizedUrl = get_bloginfo('template_url')."/libs/timthumb.php?src=".$url."&w=327&h=204&zc=1"; ?>
<img src="<?php echo $resizedUrl; ?>" alt="<?php the_title(); ?>" class="masonry-thumb" />
<?php get_template_part( 'content-issues', 'page' );?>
</div><!--issue-content--><!--Mason Item-->
<?php endif; ?>
<?php endwhile;
Now i want to display 2 posts from each post type. How can i do this ? I got the $post_count idea. But i cant know use it in my code.
Try this :
'posts_per_page' => 2,
You try This
$the_query = new WP_Query( array( 'post_type' => $post_type,'orderby' => 'post_date','posts_per_page' => 2,'order' => 'DESC'));

Resources