I just started working with WP and I'm desperately trying to create my own theme. I currently have been banging my head for a good portion of the day trying to figure out how to limit my query to just two post on my main page. For some reason it will either display all my posts or if I vary the code to have an additional value you in the array 'categories' => 'events' only one post shows up. Thanks in advance for any help you can provide :)
<?php
$args = array('posts_per_page' => 2);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) :
$my_query->the_post();?>
<?php get_template_part( 'events-content', get_post_format() ); ?>
<?php endwhile; ?>
<?php alpha_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'events-content', 'none' ); ?>
<?php endif; ?>
<?php wp_reset_postdata();?>
There are two things wrong with your Query.
The first is that you have posts_for_page in your arguments when it should beposts_per_page.
The second error is you put $args inside an array when you created the new WP_Query(). It should be:$my_query = new WP_Query( $args );
Related
Not sure that's the best way to to describe it, but here's what's happening (and I'm sure this has to be a reasonably easy thing, just not sure where to look):
Client has 10 offices so I'm using the Advanced Custom Fields plugin with a custom post type to specify a location for job postings. My code seems to be working (i.e. it's pulling the appropriate jobs by location) but it's looping the article tag the same number of times as results. I have 3 jobs in there, so it loops the whole set of results 3 times. If I delete one and drop down to 2, it loops twice, etc. I used the following example from the advanced custom fields website:
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I guess I'm not sure where to look (I realize this might be a plugin question rather than a php question) and would appreciate any direction you guys can provide.
You can see the offending page here: http://www.knrlegal.com/jobs/
You should reset the post data, not the query, as stated here:
Class Reference/WP Query, in the "Multiple Loops" section.
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'jobs',
'meta_key' => 'location',
'meta_value' => 'akron'
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<h3>Akron Office</h3>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li> <?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset with
* wp_reset_query(). We just need to set the post data back up with
* wp_reset_postdata().
*/
wp_reset_postdata();
?>
<?php endif; ?>
I'm trying to add a custom loop to display post titles from the category homepage. Here's the code I have so far so, but it's not displaying any posts. BTW, I'm using this code in single.php.
<?php $recentPosts = new WP_Query(); ?>
<?php if ( $recentPosts->have_posts() ) : ?>
<?php $recentPosts->query_posts( array ( 'category_name' => 'homepage', 'posts_per_page' => 10 ) ); ?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I'm not too sure what's going on, but would appreciate any help I can get.
You're doing a lot of unnecessary messing around with your query object. Since you're dealing with an archive page, you should be able to create a new template file called category-homepage.php (assuming the slug of the category is homepage). In it, you could place something like the following:
$args = array(
'category_name' => 'homepage',
'posts_per_page' => 10
);
$recentPosts = new WP_Query( $args );
if ( $recentPosts->have_posts() ) :
while ( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Again, since it's a category archive page, you shouldn't be doing anything to single.php. You can read more about template hierarchy in the Codex.
I'm trying to make my custom post type archive page reverse the display order, and want to make it ASCENDING. This is my code:
<?php
while ( have_posts() )
{
the_post();
?>
--MY CODE--
<?php } ?>
I tried putting query_posts('order=asc'); before the while loop, but this caused my loop to draw from the regular posts, not my custom post type.
Any help would be appreciated! Thanks
Use a simple query here. Don't use query_posts, never ever. Use WP_Query. You should do
<?php
$args= array(
'order' => 'ASC',
'post_type' => 'NAME OF YOUR CPT'
);
$the_query = new WP_Query( $args );
?>
<?php while ( $the_query->have_posts() ) :$the_querythe_post(); ?>
You need to add your ordering to the existing query. The way you did it overwrote the query. Try this:
global $query_string;
query_posts( $query_string.'&order=ASC' );
while( have_posts() ): the_post();
// rest of your code
endwhile;
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!
I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.