After implementing the code it doesn't show all the posts but the one of one specific user. However this is not the user who is logged-in but always the same user.
Here is the code i have so far:
<?php
// the query
global $current_user;
wp_get_current_user();
$author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
$wpb_all_query = new WP_Query(array($author_query)); ?>
<?php if ( $wpb_all_query->have_posts() ) : ?>
<ul>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<li><?php the_title(); ?><?php echo get_the_date(); ?><?php echo the_author(); ?></li>
<?php endwhile; ?>
<!-- end of the loop -->
</ul>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Where is the catch i don't get?
You need one wp_query object that accepts a simple array
$wpb_all_query = new WP_Query($author_query);
Related
I have a movie list to display in a page. Each movie is a post-object (which I repeat with an ACF repeater).
But in these movies, there is another post-object for the authors.
I can not see the name of the author. Can you help me ?
Here is my code
<?php while ( have_rows('sc_movies') ) : the_row(); ?>
<?php $post_object = get_sub_field('sc_movies_movie'); ?>
<?php if($post_object): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<article class="movie">
<div class="movie__content">
<h3 class="movie__title"><?= the_title(); ?></h3>
<?php $post_object = get_field('film_author'); ?>
<?php if( $post_object ): ?>
<?php $post = $post_object; setup_postdata( $post ); ?>
<span class="movie__director">Par <?= the_title() ;?> </span>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</article>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
please make sure if your "sc_movies" repeater has "sc_movies_movie" post object with "Select multiple values? = false " and also in movie post "film_author" post object is "Select multiple values? = false" then your code is correct. If you still gettting issue try add "wp_reset_query()" before while loop because may be some other post object is conflict in page.
I have registered a custom post type "Projects" and also registered a custom taxonomy for that Post Type called "Project Categories". On my home page I have a div in which I would like to list all projects and terms fro the "Project Categories" taxonomy. Currently, I am only able to get the lists of the terms. Can someone tell me why I am unable to get the terms to display. Currently, I have:
<div class="list-container">
<?php
query_posts( array( 'post_type' => 'projects' ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; wp_reset_query(); ?>
<?php $taxonomy = 'project_categories';
$tax_terms = get_terms($taxonomy);
?>
<?php foreach ($tax_terms as $cat): ?>
<li><?php $cat; ?></li>
<?php endforeach; ?>
</div><!--end list-container-->
Another question I have is, is it better to include the taxonomies inside or outside of the query_posts loop?
get_terms($taxonomy) returns a array of objects (see get_terms() in WP Codex), so in order to print the name you should use <?php echo $cat->name ?> (and don't forget the echo).
I tried to correct your code. See comments within the code block for details:
<?php
// keep your queries outside the loop for more readable code
query_posts( array( 'post_type' => 'projects' ) );
$taxonomy = 'project_categories';
$tax_terms = get_terms($taxonomy);
?>
<!-- <li> should be enclosed in <ul> or <ol> -->
<ul class="list-container">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; endif; ?>
<?php foreach ($tax_terms as $cat): ?>
<li><?php echo $cat->name; ?></li>
<?php endforeach; ?>
</ul><!--end list-container-->
<?php wp_reset_query(); ?>
Sidenote: Either you use <?php the_permalink(); ?> or you use <?php the_title(); ?>. The former will do all the magic automatically, and is recommended in this case.
I am using the relationship field within a custom post type, but my content is not showing up. I have doubled checked against the documentation but I cant see why it wouldn't be working.
Any help would be great!
Documentation here: http://www.advancedcustomfields.com/resources/relationship/
My html is:
<?php
// The Query
$args = array(
'post_type' => 'top-car'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$posts = get_field('top-cars');
if( $posts ): ?>
<ul id="recom">
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<div class="recom-single">
<h2><?php the_field( 'model' ); ?></h2>
</div>
<!--Close Recom Single-->
</li>
<!--Close First Car-->
<?php endforeach; ?>
</ul>
<!--Close Slider-->
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we have no cars available at this time, please contact us.'); ?></p>
<?php endif; ?>
I am trying to alter the number of posts that will display on an events archive page using the Events Organiser plugin. Right now the following code displays only 8 events because that is what the Wordpress query is set to.
If I create a new query and set it to post type="event" it will grab all events and not just from the event category.
How can I alter the code below to be able to make sure it pulls all posts in that event category?
This is a template file so all event categories use this template. I am trying to find an easy solution to alter the number of posts since everything else is populating correctly.
<!-- Start Loop -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-md-4 clearfix">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
?>
</a>
<div class="event__details">
<a href="<?php the_permalink(); ?>" class="event__title">
<?php the_title(); ?>
</a>
<?php
if( eo_is_all_day() ){
$format = 'd F Y';
$microformat = 'Y-m-d';
}else{
$format = 'd F Y '.get_option('time_format');
$microformat = 'c';
}?>
<time itemprop="startDate" datetime="<?php eo_the_start($microformat); ?>"> <?php eo_the_start($format); ?></time>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<!-- End Loop -->
create a custom WP_Query check this
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => 8
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
I'm having an issue with my Category template. With a number of categories it's echoing posts that aren't in the category in question. I've checked the posts and categories in my WPAdmin dashboard, they're correct; the problem lies with my code.
Side note: this loop always returns every post in the category, and then some. So it's not missing any, it's just including some that don't belong.
<?php
$categories = get_the_category(); $category_id = $categories[0]->cat_ID;
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1, 'cat' => $category_id )); ?>
<?php if ( $wpb_all_query->have_posts() and !empty($category_id) ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><br/><?php echo get_the_date('l, F j, Y'); _e(' by ');?><?php echo get_author_name(); ?></p>
<p><br/><?php _e('Categories: '); echo the_category( '/' ); ?></p>
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
<span><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?><br/>Continue Reading ></span>
<p><br/><?php the_title(); ?></p>
<p><?php echo get_the_date('l, F j, Y'); _e(' by ');?><?php echo get_author_name(); ?></p>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Alright, I have the solution. I was getting the post outside the loop, so it was defaulting to the first post. Most posts on this blog have multiple categories, and the ordering wasn't always listing the desired category first. This solution guarantees the category is grabbed based on the page loaded, rather than just the first category of the post:
<?php
$url = $_SERVER['REQUEST_URI'];
preg_match('/\/category\/(.+)\/.*/', $url, $matches);
$category_slug = $matches[1];
$category_id_actual = get_category_by_slug($category_slug)->term_id;
// the query
$wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1, 'cat' => $category_id_actual )); ?>
<?php if ( $wpb_all_query->have_posts() and !empty($category_id_actual) ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
<div>
<h2><?php the_title(); ?></h2>
<p><br/><?php echo get_the_date('l, F j, Y'); _e(' by ');?><?php echo get_author_name(); ?></p>
<p><br/><?php _e('Categories: '); echo the_category( '/' ); ?></p>
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?>
<span><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?><br/>Continue Reading ></span>
<p><br/><?php the_title(); ?></p>
<p><?php echo get_the_date('l, F j, Y'); _e(' by ');?><?php echo get_author_name(); ?></p>
<div style="float:right;"></div>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>