I have a page where I want to reference the titles from specific posts. This is my code with a loop right now -
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'cat' => '3',
);
$product_posts = get_posts( $args );?>
<p>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php echo get_the_title(); ?>
</p>
<?php endforeach; ?>
I don't want to loop through every post though, I want to be able to single out certain posts. For example, where I have the <p> get_the_title </p> I want to be able to display it like -
<p>Title of Post 5 vs Title of Post 6</p>
How can I do this?
You can try with below:
On the $catquery query the cat=3 is category ID so you can change with your specific category ID. And post_per_page=5 is total count of post so also you can change as per your required.
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=5' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
Thanks and let me know if any query.
Try this with post ID
$postid= array(144, 246);
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'post__in' => $postid,
'posts_per_page'= 5
);
// The Query
$the_query = new WP_Query( $args );
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
Related
I've got an advanced custom, outputting a chosen taxonomy, which I'm trying to pass into an array inside a WordPress loop.
The loop, which shows a specific taxonomy of my custom post type, is here:
<?php
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'portfolio_category' => 'social-media-marketing',
'posts_per_page' => -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
I would like to replace the portfolio category with the results from the custom post type, so the user can select which taxonomy to display.
The code I have to pull in the Advanced custom field taxonomy is here:
<?php
$term = get_field('portfolio_category');
if( $term ): ?>
<h2><?php echo $term->slug; ?></h2>
<?php endif; ?>
Both bits of code work separately. I've tried running them both together like this:
<?php
$term = get_field('portfolio_category');
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'portfolio_category' => 'echo $term->slug;',
'posts_per_page' => -1,
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; wp_reset_query(); ?>
As well as a few other things, but I can't seem to get it to display anything... What am I doing wrong??
Change this
'portfolio_category' => 'echo $term->slug;'
To:
'portfolio_category' => $term->slug
You were passing the variable as a string instead of a variable.
Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.
I have managed to get my loop to show only the posts that display true on an advanced custom field.
But I now only want to show one post. I cant seem to get it to only loop one of the posts that features the true/false field as yes.
'posts_per_page' => '1'
Doesn't work as it only shows the latest post.. which if its not ticked it just shows blank.
<?php
$args = array(
'post_type' => 'event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 'yes' == get_field('sponsored_event') ): ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You should use Meta Query here. Change your args array to:
// args
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1'
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
The ACF Radio button Yes/No field is to be manipulated the other way. You have to get the Output and then compare with the value that you need.
Syntax:
$variable = get_field('field_name', $post->ID);
Where the $post->ID will be appering from the Loop that you use.
if (get_field('sponsored_event') == 'yes') {
// code to run if the above is true
}
else
{
// code for else part
}
Make sure that the value saved into the DB for the radio button is like you give in the if statement
This is an Optional for you to use the meta_value in the Query. if you dod't use you can fetch the acf value with the help of the post ID that you get from the loop of Wp_Query
Change the Wp_Query like this if you need only one post that to the latest one that has been stored.
$args = array( 'post_type' => 'event', 'posts_per_page' => 1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Else if you want all the posts that has been stored you can use like this.
$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Note:
post_per_page=10 -> Will bring the 10 posts from your post type
post_per_page=-1 -> will bring infinite posts that your post type has
Entire Loop:
<?php
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Your If statement in the query seems to be Incorrect and i have added the Query Executed output over to that if statement.
Been stuck on this for days, so I thought I'd reach out.
I've created two custom taxonomies, singleclient and dualclient. These are populated with special content via Advanced Custom Fields.
What I'm looking to do is create a custom page template which joins these two taxonomies and prints out content from the two custom taxonomies.
This works for me for one of the taxonomies:
<?php
$args=array(
'post_type' => 'singleclients',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_field('client1_img'); ?>
<h3><?php the_field('client1_name'); ?></h3>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
I've tried following what the Wordpress Codex says to do, but I can't seem to get it working. Here's an example which I thought would work, but displays nothing at all.
<?php
$args=array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'singleclients',
'field' => 'slug',
'terms' => 'singleclients'
),
array(
'taxonomy' => 'dualclients',
'field' => 'slug',
'terms' => 'dualclients'
)
)
);
$posts = get_posts( $args );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_field('client1_img'); ?>
<p>print test content</p>
<h3><?php the_field('client1_name'); ?></h3>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Really pretty frustrated at this point. Anyone know what I am doing wrong and why it won't print out both taxonomies?
I did this on a website a while back:
//queries
$query1 = new WP_Query($arg1);
$query2 = new WP_Query($arg2);
//create new empty query
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
You can now access your posts as you normally would.
I am not very good at explaining stuff, so let me know if you need more info...
I couldn't get this to work, unfortunately.
What I ended up doing was creating one single taxonomy called "clients" and enabling categories for that taxonomy. This allowed me to assign "children" to clients of the types "singleclient" and "dualclient".
When I called the loop, I called one single taxonomy and then separated the output posts into two types. My code is below. I hope that this helps someone in the future.
<?php
$args=array(
'post_type' => 'clients',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if ( in_category( 'dualclient' )) { ?>
<!-- show dual client -->
<section class="two columns padding10">
<a href="<?php the_permalink() ?>">
<section class="four columns client1 dualclient_first" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
<section class="four columns client1" style="background-image:url('<?php the_field("dualclient2_img") ?>')"></section>
</a>
</section>
<?php } elseif ( in_category( 'singleclient' )) { ?>
<!-- show single client -->
<section class="two columns padding10">
<a href="<?php the_permalink() ?>">
<section class="eight columns client1" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
</a>
</section>
<?php } else { ?>
<p>Woops.</p>
<?php } ?>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Have you tried this way?
//First Query
$args=array(
'taxonomy' => 'singleclients',
'posts_per_page' => 1,
);
query_posts($args);
//Get the Posts
get_template_part( 'loop', 'index' );
wp_reset_query();
//Second Query
$args=array(
'taxonomy' => 'dualclients',
'posts_per_page' => 1,
);
query_posts($args);
wp_reset_query();
I am using a custom post type in my wordpress theme, and I need help with the loop. Here is my code:
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>
This returns the 10 latest posts in the custom field "magazine". I want it to only display the parents in the custom field "magazine". Just like pages, my custom fields have attributes, so you can select a hierarchy (parent/child). I want to edit the loop so it only returns the parents (the latest issues of the magazine, not the articles within each issue) Does anyone know how to do that using the wordpress loop above?
Just add 'post_parent' => 0 to the args array.
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10, 'post_parent' => 0 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>