I wondered how I could exclude posts in Wordpress.
E.g. I have a string
$exclude_ids (= "4,5,6") or (="-4,-5,-6")
and I would like to prevent these posts from showing up. How would I do that?
I already tried:
query_posts('p=' . $exclude_ids);
but that didn't really work out and I didn't really find any information regarding this topic on google.
Cheers
Here's the relevant info from the docs:
'post__not_in' => array(6,2,8) -
exclusion, lets you specify the post
IDs NOT to retrieve
It's in the Codex: http://codex.wordpress.org/Function_Reference/query_posts
use the post__not_in, something like: query_posts(array('post__not_in'=>'1,2,3'))
The ideal solution would be to create a category, add those posts to it, then exclude the category. But if you really want to single out posts, it could be done as follows:
<?php if (have_posts()) :
while (have_posts()) : the_post();
if ($post->ID == '179' || $post->ID == '180' || $post->ID == '181') continue;?>
<?php the_content();?>
<?php endwhile;endif;?>
Just use that if statement in your Loop. The continue will skip over that iteration for any of the listed posts.
Source: http://www.sandboxdev.com/blog/wordpress/180/exclude-single-post/
Related
I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;
I am trying to output the count of product categories that have been created for the custom post type product_cat.
When I use:
<?php
$count_posts = wp_count_posts('product_cat');
echo $count_posts->publish;
?>
it outputs "0" although there have been 3 categories created. I have searched through all of he plugin files to see how the count is output on the edit product categories page in the Wordpress admin interface. I normally refrain from asking questions due to the fact that I am new and self taught so forgive me if my terminology is not 100% correct.
Use wp_count_terms:
<?php
$product_cat_count = wp_count_terms( 'product_cat' );
echo $product_cat_count;
?>
For some reason this query is only returning the 10 most recently published results, there should be at least 15 or so. So my question is, if I don't specify the number of results in my query, will WordPress default to 10 or something?
<?php getGallery('test', 'test2'); ?>
function getGallery($galleryLink, $altLink){
global $post;
// search for any pages with a custom field of 'test'
query_posts('meta_key='.$galleryLink.'&post_type=page');
if (have_posts()) while (have_posts()) : the_post();
$link = get_post_meta($post->ID, $galleryLink, true);
$alt = get_post_meta($post->ID, $altLink, true);
$permalink = get_permalink($id);?>
<a href='<?php echo $permalink ?>'><img src="<?php echo $link ?>" alt="<?php echo $alt ?>"/></a>
<?php endwhile;
wp_reset_query();
}
default number of returned posts in $wp_query is defined in Dashboard > Settings > Reading. There you have defined 10, but remember that other people can set diffrent number on their blogs.
i hope you know that if you want to define your own number of posts you should add posts_per_page=X to query :) if you want to always return all posts matched to query put -1 in place of X
You should be able to modify the default value of 10 to any number you specify with the posts_per_page parameter in the query, like so:
query_posts('meta_key='.$galleryLink.'&post_type=page&posts_per_page=20');
Sub in any value you like instead of 20. Better yet, use a variable and stay away from literals.
Yes. The standard Loop query shows the number of posts defined in your Reading Settings page in WordPress, which defaults to 10.
See the examples under "Pagination Parameters" in the WP_Query documentation if you want to alter this programatically, rather than relying on the setting. For example:
$query = new WP_Query( 'posts_per_page=-1' );
...will retrieve all posts.
If this is your default loop, just go to Settings --> Reading and change "Blog pages show at most" to your desired number. If you specify a number that is less than your total amount of posts you'll have to include some sort of navigation to allow the visitor to go previous posts.
I'm building a small list of recent comments, and would like to make links to the actual posts that the comments were placed on. Unfortunately, there is no comment_permalink or post_permalink that I can find, so I thought maybe there would be a get_permalink() function, but again, none that I could find on http://codex.wordpress.org/Function_Reference/.
From the $post->ID alone, how can I find the permalink for that particular post? Not that it's completely necessary, but here is what I have so far:
<?php $comments = get_comments( array( 'status'=>'approve', 'number'=>5 ) ); ?>
<p class="recently-posted-comments">Recent Comments</p>
<ul>
<?php foreach ($comments as $comment): $parent = get_post($comment->comment_post_ID); ?>
<li><?php print $comment->comment_author; ?>
on <?php print $parent->post_title; ?></li>
<?php endforeach; ?>
</ul>
My intent is to convert the $parent->post_title into a permalink.
I thought maybe there would be a get_permalink() function, but again, none that I could find.
http://codex.wordpress.org/Function_Reference/get_permalink
I'd also recommend using that over get_page_link()
get_permalink() checks the post type and returns the result of the appropriate function;
Pages use get_page_link()
Attachments use get_attachment_link()
Custom post types use get_post_permalink()
The confusion comes as a result of ambiguous function names. I was looking for something that suggested a link for a "post," yet found nothing. Out of curiousity, I came across and tested get_page_link(), only to find that it does exactly what I was looking for.
Unfortunately I assumed that "page" was an exclusive term reserved for pages in wordpress, rather than posts. It appears in this context it is representative of both.
I have a WordPress site, where on the main page I list the content from more categories.
My question is, is there a plugin where I can paginate the results from a category? I mean something like $this->plugin_paginate('category_id'); or smth?
Best Regards,
If you're using the standard Wordpress loop, even with a query_posts for a category, pagination is automatic with the usual posts_nav_link. Are you trying to paginate for more than one query and more than one category on the same page?
Edit 11/20: I use this in several different places on one page to show the latest post in a category:
<?php
$my_query = new WP_Query('category_name=mycategory&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
That link then goes to a category page that paginates for that category: Category Templates « WordPress Codex
I don't know how to paginate different categories on the same page. Must be possible. Maybe ask in the Wordpress forums.
This sounds like something that a simple, well-formed query_posts() call can do. I doubt that you'll even need to rely on a plugin. :)
I'm going to assume that you're familiar with the query_posts() function, so let's go ahead and use this example as a base:
// let's get the first 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
Now, to get the 11th to 20th posts from category 3 (that is, the NEXT 10 posts), we'll want to use the [offset] parameter of query_posts():
// let's get the next 10 posts from category ID 3
query_posts('posts_per_page=10&cat=3&offset=10');
while(have_posts()):the_post();
// do Wordpress magic right here
endwhile;
For most purposes, that should be sufficient. However, you did mention that you plan on paginating category post lists from the main page alone? I'm assuming that you mean that you have multiple category post lists on your main page, and all these are paginated independently.
With something like that, it looks like you'll have to work a bit with Javascript to do the work for you, along with what I illustrated above.
I believe you could do something like this:
<?php
if(isset($_GET['paged'])){
$page = $_GET['paged']-1;
}else{
$page = 0;
}
$postsPerPage = 5;
$theOffset = $page*$postsPerPage;
?>
<?php query_posts(array('posts_per_page' => $postsPerPage, 'cat' => CATEGORIES HERE, 'offset' => $theOffset)); ?>
Hope this will help you :)
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $page,
);
query_posts($args);?>
?>