WordPress default number of results returned - wordpress

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.

Related

Fetch custom field data for a specific blog post

I want to output the metadata from a custom field in WordPress post.
On this page if WordPress codex I found the following instruction:
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
I am trying to do it this way:
<?php
get_post_meta(1, 'Currently Reading',true);
?>
But nothing gets output in the browser.
What's the proper way to output the content of the custom field?
The easiest way to do this is this:
<?php echo get_post_meta($post->ID, 'your_meta_key', true); ?>
On your post or page editor, you can go to "Screen Options" in the top right corner and check the box to display "Custom Fields". This will allow you to see the meta keys available. Just copy the name of the meta key into your get_post_meta call in the spot above where it says "your_meta_key". Don't change the $post->ID as that is global.
Taken from that page linked
<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>
so you'd need to access it through the $meta_values return object.
Like so:
<?php
print_r($meta_values);
print 'The ID is: ' . $meta_values->ID;
?>
get_post_meta(1, 'Currently Reading',true); will only get the values, you need to store it somewhere and output it properly. One way to do this is to store the function return values into a variable like so:
<?php $custom = get_post_meta( 1, $key, $single ); ?>
Then you can output it with a print or echo like so:
echo $custom;
Something to note, try using a value $post_id for the first argument. This will grab the current post id.

list wordpress post titles from one category in multiple columns in alphabetical order

I am trying to build a kind of archive of my client's projects, to display at the bottom of each page, exactly like is done in the 'Experience' section here: http://toth.com/#experience - except that in my case I only need the full list of projects, not the sub-headings or any other structure.
I have things setup so that each of my client's projects is a post. So I need a way to display the titles of the posts, from the category I've created 'Work Archive' (so that the client can add and remove things from the archive easily, by checking/unchecking the category box in each post), in vertical alphabetical order, across four columns which automatically resize to fill the container equally. Each post title in the archive also needs to link to the post, obviously.
I have been scouring the net for this for days, and while I've found pieces of code which look like they'll help, it seems impossible (with my limited PHP knowledge) to integrate them to fulfill all of my requirements. I have also looked into many WordPress plugins, with again no success. While I'll accept any solution, this is something that ideally I'd rather solve at the PHP/template level, to keep things as hidden from the client backend as possible.
Any help on this VERY much appreciated.
It sounds like the best way to do this might be to set up a new WP Query object. More info on this here:
http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$args = 'category_name=work-archive&orderby=title&order=asc&posts_per_page=9999';
// assuming 'work-archive' is the slug to your category, we are also doing ascending order by title (a,b,c,d), and pulling 9999 posts (hopefully that is more than the number of posts you have!)
// The Query
$query = new WP_Query( $args );
// Keeping track of the count
$count = 0;
// Number of items per column
$num_per_column = round($query->post_count / 4); // dividing total by columns
// The Loop
if ( $query->have_posts() ) : ?>
<ul>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php if ( $count % $num_per_column == 0 ) : // If the current count is up to it's limit throw in a new column ?>
</ul>
<ul>
<?php endif; ?>
<li><?php the_title() ?></li>
<?php $count++; // Increment counter ?>
<?php endwhile; ?>
</ul>
<?php endif;
/* Restore original Post Data */
wp_reset_postdata();
?>
Finish it off w/ some CSS!
To fix the opened ul change the condition for this :
if ( $count % $num_per_column == 0 && $count != 0)
It will prevent the closing of the ul on the first pass

How can I output the count of a specific taxonomy?

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;
?>

wordpress query (shortcode) returns always the first post

I have made a shortcode inside my plugin, which is working great .
The shortcode needs to take some parameters and create a custom loop with output.
One of the parameters is how many posts to output the loop for ($markers)
$args=array(
'meta_key'=>'_mykey',
'post_status'=>'publish',
'post_type'=>'post',
'orderby'=>'date',
'order'=>'DESC',
'posts_per_page'=>$markers,
);
$wp_query = new WP_Query();
$wp_query->query($args);
if ($wp_query->have_posts()) : while (($wp_query->have_posts()) ) : $wp_query->the_post();
// do the loop using get_the_id() and $post->id
endwhile;endif;
wp_reset_query();//END query
On occations I will need to have data from ALL posts ($markers = '-1' ) and sometimes only one ($markers = '1' ) or muliple ($markers = 'x').
All of those work great on single pages / posts - but My problem is that when this function is in a place where I have more than one post (!is_single) and ($ markers = '1' )it will always return the data for the LATEST post , and not for the correct one ..
(for example in the default wordpress theme, where it would display10 posts - they will all be the same data )
It is obviously a problem of the $post->ID - but how can I have the correct post ID when doing a custom loop OUTSIDE the wp loop ?
I tried to ovverride the problem by
global $post;
$thePostIDtmp = $post->ID; //get the ID before starting new query as temp id
$wp_query = new WP_Query();
$wp_query->query($args);
// Start Custom Loop
if (!is_single()){
$post_id_t = $thePostIDtmp;}
else {
$post_id_t = $post->ID;}
and then use $post_id_t - but it did not seems to work ,
Should I not use get_the_id() ? or should I not use query (and use get_posts) ??
Any ideas / solutions / thoughts ??
I would use query_posts(http://codex.wordpress.org/Function_Reference/query_posts)rather than override the $wp object. You should be able to include as many loops on the page as you want with this. If you have problems with this you can use: http://codex.wordpress.org/Function_Reference/wp_reset_query just before you call it.
I find this: http://blog.cloudfour.com/wordpress-taking-the-hack-out-of-multiple-custom-loops/
takes a bit of the pain away too.
There are basically two sorts of querying posts in WordPress: Those that alter the main loop and those that do not. If you want to change the main loop like the one used to display category archive pages then use query_posts. It let's you do exactly that. Delete, change and append parameters of the default query to change the outcome of a typical page.
query_posts has some drawbacks though.
Then there are queries that are just used to get stuff out of the database to play around with e.g. displaying the latest post titles in the sidebar or the attachments of the current post.
To do that create a new WP_Query object that will build your custom loop independently of the main loop like so:
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
Then there is get_posts() which is like the little brother of WP_Query. It has an easier interface in my opinion and returns an array with the results that is easier to work with.
It looks like this:
$myposts = get_posts( $args );
foreach($myposts as $post) : setup_postdata($post);
echo "<li>";
the_title();
echo "</li>";
endforeach;
Inside the foreach template tags like get_the_id() will work.

Getting Permalink from Post ID in Wordpress 3

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.

Resources