I am returning all posts on the page. Every six posts I want to incrementally show a post from a specific category. Every time this comes to pass I would like to increment up in the loop so there are no repeats of the same post within that specific category on the page.
I've successfully gotten the posts from that category showing on every sixth post on the page. I am just unable to get the loop to work so that it shows incrementally the next post in the category on every sixth spot on the page. Currently it just shows the same first post in the array.
<?php while ($query->have_posts()) {
if( $query -> post_count > 0 ) {
$postnum = 0;
foreach( $query -> posts as $post ) {
$postnum++;
if( $postnum%5 == 0 ) {
$args = array( 'cat' => 1824, 'posts_per_page' => 1, );
query_posts( $args );
$current_post = 0;
while ( have_posts() ) : the_post();
$current_post++;
echo "CTA Card Specific Info";
endwhile;
}
$query->the_post();
?>```
Instead of using query_posts(), you could nest anther WP_Query inside the first and use the offset parameter to skip over the posts you've already output. I haven't tested this code, but something like the following could work:
$post_count = 0;
$category_count = 0; // for determining offset
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'category__not_in' => 1824, // or something like this to prevent duplicates
);
$post_query = new WP_Query ( $args );
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) : $post_query->the_post();
$post_count++;
echo "Regular Post Here";
if ( $post_count % 6 === 0 ) {
$args = array(
'cat' => 1824,
'posts_per_page' => 1,
'offset' => $category_count,
);
$category_query = new WP_Query( $args );
$category_count++;
if ( $category_query->have_posts() ) {
while ( $category_query->have_posts() ) : $category_query->the_post();
echo "CTA Card Specific Info";
endwhile; $post_query->reset_postdata();
}
}
endwhile;
}
Once you've finished with the inside loop, make sure you call reset_postdata() to change the context of the query back to the main query.
It's also worth noting that using offset can mess up your pagination. I don't think that will come into play here, but if you notice pagination issues that could be the culprit.
Related
I'm trying to retrieve a single post by the ID. Stories is my custom post type and it currently has three posts:
Blog 1 (ID: 1)
Blog 2 (ID: 14)
Blog 3 (ID: 49)
I have a variable called $story_one. $story_one has the value of 14.
I'm now trying to retrieve information from the post with the ID of 14, so I've done:
<?php
$args = array(
'post_type' => 'stories',
'id' => $story_one,
'posts_per_page' => 1
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
the_title();
}
wp_reset_postdata();
}
?>
This returns Blog 3. Blog 3 is the newest post in stories. When I've specified it to pull content from the post with the ID of 14 (Blog 2), why is it. showing me content from the latest blog?
The query_var for post ID is p not id
$args = array(
'post_type' => 'stories',
'p' => $story_one,
'posts_per_page' => 1
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
the_title();
}
wp_reset_postdata();
}
Instead of using WP_Query(), you can use get_post(), like this:
<?php
$post = get_post($story_one);
if(!empty($post)):
echo $post->post_title;
echo get_field("field name", $story_one);
endif;
?>
About getting only from stories post type. Every post type is stored in the same posts table, so they have to use different post IDs.
The second parameter of get_field() function is the post ID.
I would suggest continuing to use WP_Query. The issue is just one of the query parameters.
I would ad 'p', to yours $args variable...like this...
$args = array(
'post_type' => 'stories',
'p' => $story_one,
'posts_per_page' => 1
);
And that should get you the result you're after.
Good luck!
Currently I am trying to remove duplicate iterations from my post loop, and it seems that my code is working perfectly the only issue is that my &posts_per_page=21 declare is no longer present, it displays only 3 results for example (which are the unique titles)
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'orderby' => 'title'
);
$the_query = new WP_Query( $args );
$unique_artist = array();
query_posts($query_string.'&cat=7&posts_per_page=21'); if ( have_posts() ) : while ( have_posts() ) : the_post();
// title example for post "Artist - Song Name"
$title = get_the_title();
$artist = explode('-', $title);
$main_artist = $artist[0];
$aArtist = $main_artist;
if( ! in_array( $aArtist, $unique_artist ) ) :
$unique_artist[] = $aArtist;
the_title();
endif; endwhile; endif;
How can I make it so that no matter what, my post_per_page will display the 21 desired results? Instead of only showing the unique titles, when we remove the duplicates..
I'm using this code to select related posts from the post's first category, but I need to exclude/skip one of many categories if one of them shows first.
<?php
// the query
global $post;
// We should get the first category of the post
$categories = get_the_category( $post->ID );
$first_cat = $categories[0]->cat_ID;
$the_query = new WP_Query( $args = array(
// It should be in the first category of our post:
'category__in' => array( $first_cat ),
// Our post should NOT be in the list:
'post__not_in' => array( $post->ID ),
// ...And it should fetch 9 posts
'posts_per_page' => 9,
'orderby' => 'desc'
)); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
How can I do this?
If you want to look for "related" posts unless the category in question is 81, you could do something like:
$categories_to_exclude [ 81, 11, 21 ];
$first_cat = false;
$categories = get_the_category( $post->ID );
while ( ! empty( $categories ) && false === $first_cat ) {
if ( in_array($categories[0]->cat_ID, $categories_to_exclude) ) {
array_shift($categories);
}
else {
$first_cat = $categories[0]->cat_ID;
}
}
You get the categories with get_the_category. Then in the while loop you skip the first category if it's 81, and look again. If it's not 81 (and you still have categories available), you asign it to $first_cat and carry on.
And then you only do the "related" query if you $first_cat is not false.
Getting confused from your comments, but if what you whant is to exclude posts in one or more categories from your search, you have to use category__not_in, which does accept an array. So you could do something like:
'category__not_in' => [44, 71, 85],
I am trying to query posts in wordpress using wp_query. I would like to place the posts in a category and query by using has_tag. I tried to use
$args = array (
'post_type' => array( 'post' ),
'category_name' => 'footer',
);
// The Query
$social = new WP_Query( $args );
// The Loop
if ( $social->have_posts()&&has_tag('social') ) {
while ( $social->have_posts() ) {
$social->the_post();
the_content();
}
} rewind_posts();
?>
but this loads all of the posts and doesn't just show the one with the tag.
The correct way to do this would be limit the WP_Query itself.
E.g.
$args = array(
'post_type' => array( 'post' ),
'category_name' => 'footer',
'tag' => 'social'
);
$social = new WP_Query( $args );
if ( $social->have_posts() ) {
while ( $social->have_posts() ) {
$social->the_post();
the_content();
}
}
wp_reset_postdata();
To use has_tag you'd need to set your global post data up first, e.g. setup_postdata( $social ), which would create a lot of overhead with conditions and loops just to filter your results down when you could do it within the query itself.
you need to check within each post called rather than at the start (note you might need to pass in the 2nd arg of has_tag, the post object.
if ( $social->have_posts() ) {
while ( $social->have_posts() ) {
$social->the_post();
if( has_tag('social') {
the_content();
}
}
} rewind_posts();
I am using the below query to show 4 most recent sticky posts in Wordpress.
<?php
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 4 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
ALL OF MY OUTPUTTED CODE GOES HERE - EDITED OUT TO SAVE SPACE
<?php endwhile;?>
<?php } else { echo ""; }?>
<?php wp_reset_query(); ?>
This works great but if I have a scheduled sticky post (to appear at a future date), the query ignores it as one of the sticky posts and only shows 3 - not the 4 it should?
How can I modify below code to make sure no scheduled sticky posts show AND I still retain 4 slots for sticky posts?
UPDATED CODE BELOW SHOWS ALL POSTS - NOT JUST THE MOST RECENT 4 STICKY ONES.
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'paged' => 1,
'ignore_sticky_posts' => 1
);
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
ALL OF MY OUTPUTTED CODE GOES HERE - EDITED OUT TO SAVE SPACE
<?php endwhile;?>
<?php } else { echo ""; }?>
<?php wp_reset_query(); ?>
Limit the number of posts you return in the query, not by slicing the array.
From http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
Take a look at the Codex reference above for examples of loops. Here's the gist of what you need.
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Notice how the new WP_Query accepts the args from above. In the code you posted you weren't doing anything with them.