Wordpress loop that pulls posts with related categories - php

I'm trying to create a loop that goes at the end of certain posts that will pull posts from related categories and exclude the current post. I feel like I'm close but it's still not quite working. It's just pulling all posts currently for some reason. Any help would be much appreciated.
$args = array(
'posts_per_page' => 8,
//'paged' => get_query_var('paged'),
'post__not_in' => array($post->ID),
'category__in' => $cat_ID
);
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<div style="max-width: 1350px; margin: auto;">
<div id="post-container02">
<div class="post-image"> <?php the_post_thumbnail(); ?></div>
<div class="post-feed-title"> <?php the_title(); ?></div>
<div class="read-more-button">Read More</div>
</div><!-- post-container01 -->

You may want to try this method within your main post loop. It will find the related categories and avoid the current category.
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post);
/*whatever you want to output*/
}
wp_reset_postdata();
https://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same-category

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 to show last blog post with php?

I have a this code in my WP theme and it works correctly. But when I want to show the last post from another website (for example: www.mag.tabgir.com) it doesn't show the post from my blog.
How can i change this code to show last post from this site www.mag.tabgir.com?
<section class="block-blog box">
<header>
<h2>recent post</h2>
</header>
<section class="content">
<?php
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();?>
<article class="clearfix">
<?php the_post_thumbnail('blog') ?>
<h1 class="title"><?php the_title(); ?></h1>
<span><?php the_time('d/M/Y') ?></span>
</article>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
see more
</section>
</section>
Below is the code it is working correct and tested locally.
$args = array(
'numberposts' => 1,// increase the number if you wish to display 2 latest
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
foreach ($recent_posts as $result)
{
echo 'Title : '.$result['post_title'].'<br/>';
echo 'content : '.$result['post_content'];
}

Wordpress query with two custom taxonomies

Been stuck on this for days, so I thought I'd reach out.
I've created two custom taxonomies, singleclient and dualclient. These are populated with special content via Advanced Custom Fields.
What I'm looking to do is create a custom page template which joins these two taxonomies and prints out content from the two custom taxonomies.
This works for me for one of the taxonomies:
<?php
$args=array(
'post_type' => 'singleclients',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_field('client1_img'); ?>
<h3><?php the_field('client1_name'); ?></h3>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
I've tried following what the Wordpress Codex says to do, but I can't seem to get it working. Here's an example which I thought would work, but displays nothing at all.
<?php
$args=array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'singleclients',
'field' => 'slug',
'terms' => 'singleclients'
),
array(
'taxonomy' => 'dualclients',
'field' => 'slug',
'terms' => 'dualclients'
)
)
);
$posts = get_posts( $args );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_field('client1_img'); ?>
<p>print test content</p>
<h3><?php the_field('client1_name'); ?></h3>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Really pretty frustrated at this point. Anyone know what I am doing wrong and why it won't print out both taxonomies?
I did this on a website a while back:
//queries
$query1 = new WP_Query($arg1);
$query2 = new WP_Query($arg2);
//create new empty query
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );
You can now access your posts as you normally would.
I am not very good at explaining stuff, so let me know if you need more info...
I couldn't get this to work, unfortunately.
What I ended up doing was creating one single taxonomy called "clients" and enabling categories for that taxonomy. This allowed me to assign "children" to clients of the types "singleclient" and "dualclient".
When I called the loop, I called one single taxonomy and then separated the output posts into two types. My code is below. I hope that this helps someone in the future.
<?php
$args=array(
'post_type' => 'clients',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if ( in_category( 'dualclient' )) { ?>
<!-- show dual client -->
<section class="two columns padding10">
<a href="<?php the_permalink() ?>">
<section class="four columns client1 dualclient_first" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
<section class="four columns client1" style="background-image:url('<?php the_field("dualclient2_img") ?>')"></section>
</a>
</section>
<?php } elseif ( in_category( 'singleclient' )) { ?>
<!-- show single client -->
<section class="two columns padding10">
<a href="<?php the_permalink() ?>">
<section class="eight columns client1" style="background-image:url('<?php the_field("dualclient1_img") ?>')"></section>
</a>
</section>
<?php } else { ?>
<p>Woops.</p>
<?php } ?>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Have you tried this way?
//First Query
$args=array(
'taxonomy' => 'singleclients',
'posts_per_page' => 1,
);
query_posts($args);
//Get the Posts
get_template_part( 'loop', 'index' );
wp_reset_query();
//Second Query
$args=array(
'taxonomy' => 'dualclients',
'posts_per_page' => 1,
);
query_posts($args);
wp_reset_query();

Get Wordpress posts from two categories

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.

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>

Categories