Wordpress - Two loops on author.php? - wordpress

Normally if I was doing more than one loop on a page, aside from the main wordpress loop, I'd just use wp_query, however when the author.php template is being used I can't see how I could use that, since I'd have to pass some args.
Pulling posts from categories or by date etc is all easy enough with wp_query, even getting posts by author ID can be done, but it needs to be generic, ie get posts from the current authors page.
Now, using the same loop as my category page I can easily generate posts on the author.php, but I need a second loop and I just can't figure out how to do it.
First loop would be pulling one random post and it's featured image, second would be getting the archive of that authors posts.
Any ideas?

Does this give you a list of the author's posts? If so, it should be easy to modify the arguments for WP_Query to get exactly what you want.
See here for all the options: http://codex.wordpress.org/Class_Reference/WP_Query
<?php
$new_loop = new WP_Query( array(
'post_type' => 'post',
'author' => get_the_author_meta('ID')
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; else: ?>
Sorry, nothing was found.
<?php endif; ?>
<?php wp_reset_query(); ?>

Related

How to get all the posts with some tags assigned in WordPress

I need to get the list of posts that have assigned some tags in WordPress.
For example, I choose three tags, tag1, tag2, tag3. Then I need to get the posts that had assigned tag1&tag2,&tag3, the three at once.
I'm new in WordPress then could someone help me? Thanks
You can try something like this:
<?php
query_posts( [
'post_type' => 'YOURTYPE', 'tag' => 'tag1+tag2+tag3'
] );
while (have_posts()) :
the_post();
$post->post_title;
endwhile;
wp_reset_postdata();
?>
For a complete documentation, go here: https://developer.wordpress.org/reference/functions/query_posts/

WP_Query returning unlimited number of posts, showposts and posts_per_page not working

I'm trying to hack a WordPress Theme that uses WP_Query for a "Recent Posts" widget. Currently it is limited to return only a specific Category, while I'd like it to return a limited number of posts from all categories. I have tried and succeeded at this before with other Plugins/Themes. In this case it doesn't work, irrespective of what change I try to implement, I get 4000+ posts in the query.
<?php $the_query = new WP_Query('cat='.$category.'&showposts='.$postnum);
while ($the_query->have_posts()) : $the_query->the_post();?>
What could be the common culprit of not having showposts or post_per_page working correctly as a limiter?
are you able to get a correct output from your variable $postnum ?
also try using this
$args = array('category_name' => 'my-category-slug', 'posts_per_page' => 3);
<?php
query_posts( $args );
while ( have_posts() ) : the_post();
echo 'content';
endwhile;
wp_reset_query();
?>

Wordpress loop - Display posts by tag from differing post types

I'm trying to display posts within the WP loop and am able to successfully do so with <?php query_posts('tag_id=10'); ?>Here the loop will display all posts with the tag ID of 10, but I'd also like the loop to display posts from within a Custom Post type by the same tag.
I'm able to successfully display posts with tag_id=10 that originate from a custom post type using <?php query_posts('tag_id=10&post_type=videos'); ?>
But how can I merge the two?
I gave this a shot: <?php query_posts('tag_id=10, tag_id=10&post_type=videos'); ?>
but that had no effect.
Any ideas on this one?
You can use this
query_posts(
array(
'post_type' => array('post', 'videos'),
'tag_id' => 10
));
while (have_posts()) : the_post();
// loop code
endwhile;
wp_reset_query();
This runs an action before the posts are actually queried, thus altering the original output to your specific needs.
function tag_archive_mod( $query ) {
if ( is_tag() && $query->is_main_query() ){
$query->set('post_type',array('post','video'));
}
}
add_action('pre_get_posts', 'tag_archive_mod');
Very, very useful.
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Wordpress search field affecting custom query

Just came across an issue with my Wordpress template that I can't figure out. I am using a custom built menu (done simply with a quick query_posts() call), but when searching for certain terms, my query is affected. Not a clue as to why.
Here's my menu code:
<?php $main_cats=explode(",",$options['main_cats']); ?>
<?php $myargs = array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'); ?>
<?php query_posts($myargs);
while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_query();?>
This should end up with 4 menu items. However, when searching for a few "job" related items, it seems that the Job Manager plugin steps in (http://pento.net/projects/wordpress-job-manager-plugin/) and I get one menu result stating "This job doesn't exist". However, I don't understand how that plugin could possible affect my query.
Any thoughts?
This code assume that $main_cats has the right data, before testing this, check that the variable is correct:
<?php
$main_cats=explode(",",$options['main_cats']);
$menu_items=get_posts(array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'));
foreach($menu_items as $menu_item){ ?>
<li><?php echo $menu_item->post_title; ?></li>
<?php } ?>
Maybe Use native menu function to to create menu's, http://codex.wordpress.org/Function_Reference/wp_nav_menu
Don't use query_posts($myargs), query_posts() is meant for altering the main loop.
Use WP QUERY http://codex.wordpress.org/Class_Reference/WP_Query (or get_posts).
Try wp_reset_query(); before your query as well as after, like you have. I think that the plugin query is happening before your yours. There may be some carry over from that. Reseting before your query might give you a clean slate.

How can I display a list of WordPress custom posts?

I am using WordPress 3, and I created a custom post type called article, which gives me the URL format of mywebsite/articles/article-title. How do I see all the article entries in the URL mywebsite/articles?
Assuming you set up everything correctly and you want to see the post type on a public template page, try this code into mycustomtemplate.php or the equivalent.
<?php $loop = new WP_Query( array( 'post_type' => 'article', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
You can customize the loop just like you would blog posts and pages. If you want to get all on one page you'll want to remove the limit of 10 on post_per_page I wouldn't suggest it though. I would say set it to 50 or 100 and still use pages.
Source: Custom post types in WordPress
I have the simplest solution. Just create file archive-{custom post type}.php and then, just do loop content as usual.
If you already set the permalink, just type yourdomain.com/{custom post type}.
You can easily achieve this from the definition of your custom post type, starting with WP 3.1. Just set has_archive to true.
Source: http://codex.wordpress.org/Post_Types#URLs_with_Namespaced_Custom_Post_Types

Resources