<?php
if( have_rows('repeater_field_name')): // check for repeater fields ?>
<?php while ( have_rows('repeater_field_name')) : the_row(); // loop through the repeater fields ?>
<?php // set up post object
$post_object = get_sub_field('test');
if( $post_object ) :
$post = $post_object;
setup_postdata($post);
?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
<?php // whatever post stuff you want goes here ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<!-- End Repeater -->
<?php endif; ?>
Hi, Im trying my best to add an Bidirectional inside a repeater. Can anyone help me? this is my query.
This is the query i want to inside above the code idk if possible but im hoping..
$related_articles = get_posts(array(
'post_type' => 'book',
'meta_query' => array(
array(
'key' => 'relationship_field', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
Related
I am creating a property site where there is a featured property on the home page. To define a property as featured I have created an acf checkbox with the value as Yes when checked. I have tried filtering the posts by checking if the checkbox is checked but I cannot figure it out. Here's my code which isn't working;
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => 1,
'meta_key' => 'featured_property',
'meta_value' => 'Yes'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ) : ?>
<?php
$main_field = get_field('images');
$first_row = $main_field[0];
$img = $first_row['image'];
$img_crop = $img['sizes']['fresh_size'];
?>
<img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php wp_reset_postdata(); ?>
<?php endif; ?>
READ THIS: for anyone attempting to do this with a checkbox like I was don't. after a little research i found out "Checkboxes are stored as serialized data and you’re not going to be able to use WP_Query to filter by a checkbox field" Use true / false instead and check if the value equals '1' or '2' depending on what you are trying to achieve.
https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/
Remove this part:
'meta_key' => 'featured_property',
'meta_value' => 'Yes'
Instead, filter out who has the checkbox checked inside the loop. You are also missing parts of the loop. Try this code:
<?php if( $query->have_posts() ) : ?>
(...)
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if( get_field('featured_property') ) { // << FROM HERE ?>
<img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php } // << TO HERE ?>
<?php endwhile; ?><!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have cut out the first part of your code to make it easier to read.
--
Or, if you would like to use meta_key instead, try adding:
'compare' => 'EXISTS'
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => 1,
'meta_key' => 'featured_property',
'meta_value' => 'Yes'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ): ?>
<ul>
<?php while( $query->have_posts() ) : $query->the_post();
$images = get_field('images');
$first_row = $main_field[0];
$img = $first_row['image'];
$img_crop = $img['sizes']['fresh_size'];
?>
<img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post().
?>
Please check your settings under acf checkbox "Choices", check if it "Yes : Yes" or "yes : Yes" and fix your 'meta_value' if you have "yes : Yes" to 'meta_value' => 'yes'. Checkbox saves data as value-label. I think you have issue with your configuration of checkbox.
What type of 'images' field do you use? Is it repeater or gallery? If you using gallery then for src of the image you need to use:
$images = get_field('images');
$img_crop = $images[0]['sizes']['fresh_size'];
I have this template part:
<?php
while (have_posts()) : the_post();
get_template_part('template-parts/content', 'page');
do_action('flash_before_comment_template');
// If comments are open or we have at least one comment, load up the comment template.
if (comments_open() || get_comments_number()) :
comments_template();
endif;
do_action('flash_after_comment_template');
endwhile; // End of the loop.
?>
And I have a Post Type named food. I want to show only thoose posts (post with title and whole body) which contains this Post type. How should I modify the template to make it work?
Custom post type get the query
<?php
$custom_args = array(
'post_type' => 'food',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page'=> -1
);
$get_news_query = new WP_Query( $custom_args ); ?>
<?php if ( $get_news_query->have_posts() ) : ?>
<?php while ( $get_news_query->have_posts() ) : $get_news_query->the_post(); ?>
<h2> <?php the_title(); ?> </h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_query();
?>
I have created a custom post type called news and added certain news. Now i want to display it in archive page. I have added the 'has_archive' => 'true' in the functions file.
My archive page code is:
<?php
$args = array('post_type' => 'news',
'post_status' => 'publish');
$news=wp_get_recent_posts($args);
?>
<div class="container mc_tb_p">
<h1>NEWS/RELEASES</h1>
<?php
foreach ($news as $row)
{
$id = $row['ID'];
$ntitle = $row['post_title'];
$ncontent = $row['post_content'];
$ncontent = strip_tags($ncontent);
if (strlen($ncontent) > 100)
{
$stringCut = substr($ncontent, 0, 200).'... Read More';
}
else{
$stringCut = $row['post_content'];
}
$ndate = $row['post_date'];
$trim = new DateTime($ndate);
$trimdate = $trim->format('d-m-Y');
// var_dump($trimdate);
?>
<div class="news_releases">
<h3><?php echo $ntitle?></h3>
<h5><i>Published On: <?php echo $trimdate?></i></h5>
<p><?php echo $stringCut;?></p>
</div>
<?php
}
?>
</div>
now when i give my url:https//sitename/news ... its bringing up the single page of the 2nd news and nothing else , i have tried everything but nothing seems to work.Please help
As with most weird routing errors when using custom post types: saving the permalinks solves this problem (at least for me it did)
Just go to the settings/permalinks and click save
You have two option,
1. Create a file name as " archive-{post_type}.php " in current active theme folder and use below code in that file,
<?php if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile;
endif; ?>
REF : https://codex.wordpress.org/Post_Type_Templates
OR
2.Create custom template file and use below code,
<?php
$loop = new WP_Query( array( 'post_type' => 'posttypename', 'paged' => $paged ) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="pindex">
<?php if ( has_post_thumbnail() ) { ?>
<div class="pimage">
<?php the_post_thumbnail(); ?>
</div>
<?php } ?>
</div>
<?php endwhile;
if ( $loop->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">←</span> Previous', 'domain' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">→</span>', 'domain' ) ); ?></div>
</div>
<?php endif;
endif;
wp_reset_postdata();
?>
*posttypename - ypu post type name
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'deals',
array(
'labels' => array(
'name' => __( 'Deals' ),
'singular_name' => __( 'Deal' )
),
'public' => true,
'has_archive' => true,
)
);
}
=>The best way to start would be to copy the code from your theme’s archive.php file and paste it in your archive-{posttype}.php file. Then start tweaking from there. You can style this archive file to your heart’s desire. A very basic template would look like this
<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile; endif;
get_footer();
?>
Reference : http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/
I am trying to display all posts in the category on the category archive page.
I have used the following code, but it displays all posts from all categories.
Can someone help me please?
<?php
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Thanks
You need to add a category parameter to the WP_Query array.
Check this reference in the "Category Parameters" section: https://codex.wordpress.org/Class_Reference/WP_Query
Something like this should work:
$wpb_all_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'your_category_slug'
));
I've got a wordpress theme with a set of custom fields.
One of these is named "author".
On single.php I've got a div which show the other posts with the same custom field value.
I would like to display this div only if exists other posts with the same custom field value, else I would like to display nothing.
Thanks for your help!!
This is my actual code:
<?php
$myquery = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
if ( $myquery->have_posts() ) : ?>
<div class="related">
<h3>Altre di <?php the_field('autore'); ?></h3>
<ul>
<?php while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
<?php
echo '<li>'; ?>
<?php
$fotorel = get_field('foto_homepage');
list($width, $height) = getimagesize("$fotorel");
$relheight = $height / 2;
?>
<div class="related-foto" style="background:url(<?php the_field('foto_homepage'); ?>) no-repeat center center; height:<?php echo $relheight.'px' ?>"></div>
<?php the_title(); ?>
<?php echo '</li>';?>
<?php endwhile; ?>
<?php else : // What to do if there are no posts from that author
endif;?>
</ul>
</div>
<?php wp_reset_query(); ?>
Here an example:
http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
Using the condition <?php if ($pageposts): ?> you can print your div or not.
I'm not sure how you are querying for the posts in the custom fields but the $wp_query has built in conditionals for handling queries that don't return posts.
Updated code sample:
$args = array(
'meta_key' => 'autore',
'meta_value' => $autore,
'showposts' => 2,
'post__not_in' => array($post->ID)
);
$your_query = new WP_Query( $args );
if ( $your_query->have_posts() ) : ?>
<div id="your-div">
while ( $your_query->have_posts() ) : $your_query->the_post();
// Do stuff
endwhile;
else : // What to do if there are no posts from that author
endif;