Related post by custom field - wordpress

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

Related

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(); ?>

WP_Query post__not_in is not working. Ignores the array

I've been through other posts on here and elsewhere and compared my code to working examples I've done previously, but I can't find what the issue is. I;m using the following query to grab a featured article and store its ID:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 564,
);
$featured_latest = new WP_Query($args);
$fid = array();
if( $featured_latest->have_posts() ) : while( $featured_latest->have_posts() ) : $featured_latest->the_post(); ?>
<?php $fid[] = get_the_ID(); ?>
<div class="top-featured">
<?php if( has_post_thumbnail() ) { ?>
<?php $image = get_the_post_thumbnail_url(get_the_ID(),'large'); ?>
<?php } else { ?>
<?php $image = get_stylesheet_directory_uri() . '/assets/images/blog/no-article-image.jpg'; ?>
<?php } ?>
<a class="article-link lazy" href="<?php the_permalink(); ?>" data-src="<?php echo $image; ?>">
<div class="text">
<h2><?php the_title(); ?></h2>
<?php
$date = get_the_date();
$cdate = date( 'c', strtotime($date) );
?>
<time datetime="<?php echo $cdate; ?>"><?php echo $date; ?></time>
<div class="excerpt">
<?php echo get_excerpt(140); ?>
</div>
<span class="fake-link">Read article</span>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
Then lower down the page I use this query:
<?php
if( !empty( $fid ) ){
$fid = $fid;
} else {
$fid = array();
}
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 399,
'post__not_in' => $fid,
);
$top_reviews = new WP_Query($args);
if( $top_reviews->have_posts() ) : ?>
<div class="top-articles top-reviews">
<h2>Top product reviews</h2>
<div class="row">
<?php while( $top_reviews->have_posts() ) : $top_reviews->the_post(); ?>
<div class="col-12 col-md-4">
<?php if( has_post_thumbnail() ) { ?>
<?php $image = get_the_post_thumbnail_url(get_the_ID(),'large'); ?>
<?php $imgid = get_post_thumbnail_id( get_the_ID() ); ?>
<?php $alt = get_post_meta( $imgid, '_wp_attachment_image_alt', true); ?>
<?php } else { ?>
<?php $image = get_stylesheet_directory_uri() . '/assets/images/blog/no-article-image.jpg'; ?>
<?php $alt = 'No article image'; ?>
<?php } ?>
<a class="article-link" href="<?php the_permalink(); ?>" data-src="<?php echo $image; ?>">
<img class="lazy" data-src="<?php echo $image; ?>" alt="<?php echo $alt; ?>">
<div class="text">
<h3><?php the_title(); ?></h3>
<?php
$date = get_the_date();
$cdate = date( 'c', strtotime($date) );
?>
<time datetime="<?php echo $cdate; ?>"><?php echo $date; ?></time>
<div class="excerpt">
<?php echo get_excerpt(140); ?>
</div>
<span class="fake-link">Read article</span>
</div>
</a>
</div><!-- col -->
<?php endwhile; wp_reset_postdata(); ?>
</div><!-- row -->
</div>
<?php endif; ?>
$fid prints as an array with one item inside, but the second query does not exclude the post of that ID. I'm sure there's something glaringly obvious that I'm missing, but I can't for the life of me find it!
May be some other plugin or theme features overrides the array? var_dump and try what are the query_vars in the WP_Query object. then you can identify it. Or simply deactivate all the plugins and activate it one by one. then you can identify surely!
if any plugin overrides it, they should use a hook to do it. so search and see the priority of the hook. and then just you override it with high priority hook. example below.
add_action('pre_get_posts', 'your_callback_function_name', 999)
function your_callback_function_name($query){
$overridden_array = $query->get('post__not_in', array());
$overridden_array[] = $your_f_id;
$query->set('post__not_in', $overridden_array);
}
Thats all. Just rename those functions and variables as your wish.
You need to pass array in this format array('162','3074')
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'post__not_in' => array('162','3074')
);
$top_reviews = new WP_Query($args);
Its working fine for me.. you can try in same way.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Change post__not_in value into Array.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'desc',
'post_status' => 'publish',
'cat' => 399,
'post__not_in' => array('YOUR_POST_ID1','YOUR_POST_ID2'),
);
For more help see this link : Click Here
When printing out the WP_Query resuls, I found that the following was happening:
WP_Query Object ( [query] => Array ( [post_type] => post [posts_per_page] => 3 [orderby] => date [order] => desc [post_status] => publish [cat] => 399 [post__not_in] => Array ( [0] => 8117 ) ) [query_vars] => Array ( [post_type] => post [posts_per_page] => 3 [orderby] => date [order] => DESC [post_status] => publish [cat] => 399 [post__not_in] => Array ( [0] => 7990 )
It was suggested to me by dineshkashera that the pre_get_posts() filter may have been the cause. As I was not using this myself, I troubleshooted for plugin conflicts by deactivating all those that were not regular ones I used. After reactivating them one at a time, I found that the culprit was the Woocommerce Point of Sale plugin. I have deactivated this plugin for now and will seek a solution from the developer.

Advanced custom field query in 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 );

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.

how to query_posts by category slug

I am using the following code to list some pages on my wordpress site:
$args = array( 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);
?>
<?php query_posts($args); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>" id="prod-link">
<?php if( has_sub_field('images') ): ?>
<?php $img = get_sub_field('image') ?>
<img src="<?php echo $img['sizes']['product-index-pic'] ?>" />
<?php endif; ?>
</a>
<?php endwhile; ?>
<!-- #posts -->
<div class="pagination">
<?php posts_nav_link( ' ', '<img src="' . get_bloginfo('template_url') . '/assets/images/prev.jpg" />', '<img src="' . get_bloginfo('template_url') . '/assets/images/next.jpg" />' ); ?>
</div>
<!-- .pagination -->
I am wondering if there would be a way to limit those based on a certain category slug?
Thanks in advance
You can use the variable category_name like this:
$args = array( 'category_name' => ***YOUR CATEGORY SLUG***, 'posts_per_page' => 12, 'order'=> 'ASC', 'post_type' => 'tcp_product', 'paged' => $paged);
tax_query is used to get the posts associated with certain taxonomy.
{tax} (string) - use taxonomy slug. Deprecated as of Version 3.1 in favor of 'tax_query'.
tax_query (array) - use taxonomy parameters (available with Version 3.1).
taxonomy (string) - Taxonomy.
field (string) - Select taxonomy term by ('id' or 'slug')
terms (int/string/array) - Taxonomy term(s).
include_children (boolean) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
operator (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
$args = array(
'post_type' => 'tcp_product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'tcp_product_taxonomy',
'field' => 'slug',
'terms' => 'your-cat-slug'
)
)
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post;
// do something
}
}

Resources