Get Wordpress posts from two categories - php

I am trying to display posts from two categories on one page (6 & 7). It should include posts from either category as well as the ones matching both. Unfortunately I have so far only found solutions for the latter which is why I am asking you for help now. This is my current code:
<div class="sponsoren_container_wrapper">
<?php $args = array ( 'posts_per_page' => -1, 'category' => 6 );
$myposts = get_posts ( $args );
foreach ($myposts as $post) : setup_postdata( $post ); ?>
<div class="gallery_image_container">
<a href="<?php
$content = get_the_content();
print $content;
?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
How would I be adding the category ID "7" to that code to make everything work as explained above?
Thanks in advance!

use a comma seperated string:
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
http://codex.wordpress.org/Template_Tags/get_posts

$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
$myposts = get_posts ( $args );

$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
$myposts = get_posts ( $args );
For me, 'category' would not pass but 'cat' did. I hope this helps someone.

Related

why the get_posts function displays all the articles except one?

This get_posts function displays all the articles of the categories except one.
numberposts => -1 is specified to retrieve all the articles in a category.
why am I not showing them all?
Thx!
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => -1, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post )
{ setup_postdata($post); ?>
<div class="article">
<a class="article_link" href="<?php the_permalink(); ?>" title="En savoir plus"><?php the_title(); ?></a>
</div>
<?php
} wp_reset_postdata(); ?>
Because in your get_posts() function, you are passing the parameter:
'post__not_in' => array($post->ID)
So the get_posts() would retrieve all posts, except the current one (which you are passing via $post->ID)
Here's the screenshot of the documentation:

How can I show specific posts on a WordPress page?

I have a page where I want to reference the titles from specific posts. This is my code with a loop right now -
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'cat' => '3',
);
$product_posts = get_posts( $args );?>
<p>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php echo get_the_title(); ?>
</p>
<?php endforeach; ?>
I don't want to loop through every post though, I want to be able to single out certain posts. For example, where I have the <p> get_the_title </p> I want to be able to display it like -
<p>Title of Post 5 vs Title of Post 6</p>
How can I do this?
You can try with below:
On the $catquery query the cat=3 is category ID so you can change with your specific category ID. And post_per_page=5 is total count of post so also you can change as per your required.
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=5' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
Thanks and let me know if any query.
Try this with post ID
$postid= array(144, 246);
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'post__in' => $postid,
'posts_per_page'= 5
);
// The Query
$the_query = new WP_Query( $args );
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>

Sort posts in WordPress by list of author IDs / Combine two loops

I want to sort a list of posts based on the IDs of a few authors.
The posts from this authors should appear on top of the post list (in alphabetical order). After these posts, the rest of the posts should also appear in alphabetical order.
I found no easy way to sort by a list of authors.
At the moment I've a list of author IDs in an array. The code for that looks like this:
<ul>
<?php
$posts = get_posts( array(
'post_type' => 'post',
'numberposts' => -1,
) );
$ids = array();
foreach ( $posts as $post ) :
setup_postdata( $post ); ?>
<?php
$post_author_id = get_post_field( 'post_author', $post_id );
array_push( $ids, get_the_author_meta('ID', $post_author_id) );
?>
<?php endforeach; wp_reset_postdata(); ?>
</ul>
<?php print_r($ids); ?>
And the output looks like this:
Array ( [0] => 545 [1] => 543 [2] => 542 [3] => 538 [4] => 6 )
Now I've my normal WordPress Loop which looks like this:
<?php
$wp_query = new WP_Query();
$wp_query->query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '-1',
'orderby' => 'title',
'order' => 'ASC',
)
); ?>
<?php global $wp_query; if ( have_posts() ) : ?>
<div>
<?php while ( have_posts() ) : ?><?php the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; ?>
</div>
<?php endif; $wp_query = null; $wp_query = $temp; wp_reset_query(); ?>
I didn't found a way in the Class Reference (https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters) to order by a list of author IDs. Is there any solution to do this?

Wordpress Taxonomy listing - Pagination

I found this excellent function to display all the posts listed under a specific custom taxonomy. It works great. Found here http://gilbert.pellegrom.me/wordpress-list-posts-by-taxonomy I tried a number of ideas, however unsuccessful, to try and paginate the returned data. I either get no data or it continues to display the entire list. Some of my taxonomies have over 10K posts associated. So pagination would seem logical.
What I want to do is; have the information that gets returned create pages of 'n' number of posts and make links for the other pages (1,2,...4,5 etc). Any help is greatly appreciated.
I tossed this in my functions file;
function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(),
$wp_query_args = array() ){
$tax_terms = get_terms( $taxonomy, $get_terms_args );
if( $tax_terms ){
foreach( $tax_terms as $tax_term ){
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
);
$query_args = wp_parse_args( $wp_query_args, $query_args );
$my_query = new WP_Query( $query_args );
if( $my_query->have_posts() ) { ?>
<h2 id="<?php echo $tax_term->slug; ?>" class="title">
<?php echo $tax_term->name; ?></h2>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
}
wp_reset_query();
}
}
}
?>
And this code goes in the template, stuff whatever 'taxonomy' name and it displays the data. Another questions I was not sure about, if the pagination should go in the function or the template.
<div class="my_class">
<?php
list_posts_by_taxonomy( 'my_posttype', 'taxo_mytaxo' );
?>
</div>
Thank you everyone!
In order to have pagination with your query first of all you have to use the paged parameter.
To get some extra info check the wordpress codex for Pagination
Usually you get the pagination variable like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
Then you pass it to the query by including it in the query arguments for your code:
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
'paged' => $paged //I've added it here
);
Then you'll have to build the pagination links something like(this will be done inside the loop):
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

Random Post ruining my Wordpress comments section

I currently have this code on my template:
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
The nature of the code is to generate a list of Random post from blog post. The problem is, the code starts ruining my comments section by displaying the wrong list of comments to unrelated blog post.
see my sample on the link above
The common sense to do, is to remove to code in my template. My question is any ideas on how to fix the code above so i can still use it?
In case you work with get_posts, and you need to override the $post, you'll have to do it like this:
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
<?php $post = $tmp_post; // reset the $post to the original ?>

Categories