Retrieve certain posts by their number? - wordpress

How can I call certain posts by their number to place them as featured posts in specific places of my template ?
I've read about sticky posts , But I have about 3 places to place my featured posts and I guess I need a loop to get posts by their number I defined.
Any HELP ?!

<?php
// The Query
$query = new WP_Query( 'p=7' );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>

Related

How to Display post from specific tag on top of category page in wordpress

I want to create a div, like an editor choice zone, that display the post tagged "editor-choice" on the top of each category page. Of course it will only display post from current category page. Thanks
If some one is interested, here is the code:
<?php
if (is_category( )) {
// this grabs the current category slug
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
}
// this is the query to sort the tag only with the CURRENT CATEGORY by slug
$args = 'tag=editor-choice&category_name='.$yourcat->slug.'&category_title='.single_cat_title( '', false );
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
wp_reset_query();
?>

Listing Post Ids within loop on page?

Is there anyway to list the post IDs for all the posts in the loop on a page in Wordpress?
Id like to make a navigation bar out of the current posts on the page.
Wordpress 3.3
Thanks!!
Use query_posts() to get all posts and loop through it for ids, example:
<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>

Display posts out of wordpress

I need to get some posts by category and display them out of the wordpress. I need these posts hidden in the wp blog. But visible in my custom page. Via php.
I'm noob with wordpress. Can anyone direct me about how can I do that?
Your file should be something like this:
<?php
require '/path/to/wordpress/root/wp-load.php';
$the_query = new WP_Query( 'post_status=private' );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
?>
Read more about WP_Query class to get posts which you need.

Dynamically display posts by one author AND from one category on the same page?

I'm trying to edit my author.php wordpress template so that it shows posts by any one author, but only from one particular category. So far, I've been trying the query_posts function which fetches the category okay, but not the author. Depending on which way I do it, so far the posts either don't display at all or all posts in that category appear regardless of the author.
This is the appropriate code which I've seen quoted by a wordpress.org admin, but it doesn't work for me and I can't find any other examples. Any ideas why that doesn't work? Thanks for your help in advance.
//Gets author info to display on page and for use in query
<?php
$curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author'));
?>
//Queries by category and author and starts the loop
<?php
query_posts('category_name=blog&author=$curauth->ID;');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
//HTML for each post
<?php endwhile; else: ?>
<?php echo "<p>". $curauth->display_name ."hasn't written any articles yet.</p>"; ?>
<?php endif; ?>
============ ALSO TRIED ============
<?php
new WP_Query( array( 'category_name' => 'blog', 'author' => $curauth->ID ) );
?>
This doesn't work either, however it does filter the posts by author, just not by category! What am I doing wrong?
Thanks!
This task can be done using pre_get_posts filter. By this way it's also possible to filter for author in addition than for category:
// functions.php
add_action( 'pre_get_posts', 'wpcf_filter_author_posts' );
function wpcf_filter_author_posts( $query ){
// We're not on admin panel and this is the main query
if ( !is_admin() && $query->is_main_query() ) {
// We're displaying an author post lists
// Here you can set also a specific author by id or slug
if( $query->is_author() ){
// Here only the category ID or IDs from which retrieve the posts
$query->set( 'category__in', array ( 2 ) );
}
}
}
I just had this same issue, which I solved using a check for if(in_category('blog')) after the loop started, like this:
if ( have_posts() ) : while ( have_posts() ) : the_post();
if(in_category('blog')) {
?>
<!-- Loop HTML -->
<?php } endwhile; else: ?>
<p><?php _e('No posts by this author.'); ?></p>
<?php endif; ?>
Of course the $curauth check would come before this.

Wordpress loop > unique loop renders slightly wrong results

A few things to understand before my question will make sense:
I use a hidden category called 'Unique' to specify if the post will use the single.php or a special one used for the unique ones.
I want the index to act as a single: showing only one post, displaying next/prev post links, and comments also.
I need the index.php to say if the post is in category 15 (unique) than <the_unique_content>, else; <the_default_content>
My loop does all this, but the problem is that if the current post is unique, it also displays 1 additional post below the unique post.
Here is the loop >
<?php $wp_query->is_single = true; ?>
<?php $post_count = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($post_count == 0) : ?>
<?php if (in_category('15')) { ?>
<?php the_content(); ?>
<?php } else { ?>
<?php the_content(); ?>
<?php $post_count++; ?>
Thanks for any help!
I don't think you are setting the query correctly to return a single post. Your code is limiting the number of posts through your $post_count variable, but in the case where the post is "unique", it only increments to 1 on the second post.
Here is one way to limit the number of posts to a single post. It involves modifying the loop query to set the number of posts per page to one.
<?php
global $wp_query;
$new_query = array_merge( array( 'posts_per_page' => 1 ), $wp_query->query );
query_posts( $new_query );
if (have_posts()) : while (have_posts()) : the_post(); ?>
etc...

Resources