I'm using the basic loop code in a taxonomy archive (artists) and I was wondering how you can set the loop to show posts in random order ('orderby'=>'rand') it doesn't seem to work when I add the array? Any help would be great!
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
array ( 'orderby' => 'RAND' );
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
<?php
$query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '-1' ) );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
more info for query
query_posts(array(
'showposts' => 6,
'orderby' => 'rand',
'category_name' => 'News' //You can insert any category name
));
Nice Question first !
You can do that with simple using function of PHP.
http://www.php.net/manual/en/function.shuffle.php
Follow below step:
First thing is get all post with query
You know that wordpress will provide the result in array format. So, do not try more coding its too complecated.
Now you have array of result so just use PHP function shuffle.
http://www.php.net/manual/en/function.shuffle.php
Please ask me after implementation if any query.
Thanks !
Try this:
<?php
$args = array(
'orderby' => 'rand'
);
$query = new WP_Query($args);
if (have_posts()) {
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
You have two ways of doing it. The first way is not the best way, but it may be simpler for you to understand:
Using WP_Query
<?php
$args = array(
'orderby' => 'random'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
// Start the Loop.
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
Here, we'll be using a custom WP_Query object and orderby to get random posts.
Using pre_get_posts
The best way to do it is by using the pre_get_post action to modify the page output automatically. You might need some more coding though.
Related
I have a custom taxonomy called mwp_ss_supp. I have a page that displays all the posts using this code:
<?php $count = wp_count_posts('mwp_ss_supp')->publish;
echo '<div id="post-count">' . $count . ' posts displayed</div>';?>
<?php
if ( have_posts() ) :
while (have_posts()) : the_post();
DO STUFF
<?php
endwhile;
endif; ?>
The problem is that all the posts are displaying in the order of latest post is the first on the list published. But I want to print them in a different order (ie, posts 1-10 are published first, posts 15-20 are published next, and 11-14 are published last, which ends up being published in alphabetical order by title). So how do I access this array of posts? There doesn't seem to be an array variable to play around with.
Show sql query for that. The sql query gets data from the database and determines the sort
Try adding a pre_get_posts() modifier to your theme's functions.php:
function taxo_posts($query)
{
if ($query->is_tax('mwp_ss_supp') && $query->is_main_query())
{
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' ); // or DESC
}
}
add_action('pre_get_posts', 'taxo_posts');
You need to build a custom wordpress query WP_Query() to query posts against taxonomies:
<?php
$args = array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy_type',
);
);
'orderby'=> 'title',
'order' => 'ASC',
);
/**
* WP_Query()
* The WordPress Query class.
* #link https://developer.wordpress.org/reference/classes/wp_query/
*/
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
if( get_the_title() !== '' ):
the_title( '<h1>', '</h1>' );
endif;
if( get_the_content() !== '' ):
the_content();
endif;
endwhile;
endif;
/**
* wp_reset_postdata()
* After looping through a separate query, this function restores the $post global to the current post in the main query.
* #link https://developer.wordpress.org/reference/functions/wp_reset_postdata/
*/
wp_reset_postdata(); ?>
Learn more
wp_query # https://developer.wordpress.org/reference/classes/wp_query/
I need to query my pages to display on blog posts. But not all pages are going to be displayed so I've use metabox and create conditional checkbox if its going to be displayed or not.
My problem is when I query the post and pages, it doesn't show the posts/pages that are allowed to be shown on the blog page.
Assume that the meta_value is 1 which is set not to display and meta_value 0 is set display.
Below is my code:
$args = array(
'post_type' => array('post', 'page' ),
'meta_key' => 'gz_checkbox',
'meta_value' => '1',
'meta_compare' => '!='
);
query_posts($args);
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
In my Wordpress site, my taxonomy-product-category.php template has the following loop for loading posts into the page:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
However, I want the posts to be randomly ordered rather than following a specific order like date added.
How can I modify this loop to do it?
You can use pre_get_posts to set random ordering to your taxonomy pages. Just note, random ordering duplicates posts between paged pages as each page is a new query and not an extension to one. This is unfortunately how random ordering works.
You can try the following
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // Only targets the front end
&& $q->is_main_query() // Only targets the main query
&& $q->is_tax( 'product-category' ) // Only targets the product-category tax pages
) {
$q->set( 'orderby', 'rand' );
}
});
Change:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
To:
<?php
$args = array(
'cat' => YOUR CATEGORY ID,
'post_type' => YOUR CUSTOM POST TYPE,
'orderby' => 'rand'
);
$query = new WP_Query($args);
?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
I am building a one page WordPress template. I have created a simple loop that pulls all the pages and displays each one in succession on the same page. It displays the content for each page but dose not apply the template that has been attached to the page.
Here is the loop...
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
When you use the_content, the function will only return the value for the filed post_content within the table. So the code is working as it is expected.
Try the following
if ( have_posts() ){
while ( $the_query->have_posts() ) {
$the_query->the_post();
locate_template( get_template_slug( $post->ID ) )
}
}
This code uses the function locate_template to find the template file, and get_template_slug to get the name of the template associated with each page.
Let me know if this helps.
I am creating a website that integrates a portfolio which uses custom post types, this was done based off of this tutorial.
So far it is exactly what I am looking for and works great except for one small detail. In order to fetch the posts from the new custom post type the author of the tutorial used the query_posts() codex. So the top of my portfolio page looks like this:
<?php
/* Template Name: Portfolio */
get_header();
query_posts('post_type=portfolio&posts_per_page=10');
?>
What I gather is that this declares "get posts from "post type" portfolio and show 10 per page". My problem is that I can't go get content from my portfolio page. It seems that now my portfolio page only fetches the content from the custom post type, and I can't use:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
to get content from the actual page.
This is what I am trying to do, I've replaced:
query_posts('post_type=portfolio&posts_per_page=10');
with:
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_page( 8 ) && $query->is_main_query() )
$query->set( 'post_type', array( 'portfolio' ) );
return $query;
}
This seems like the right track, but it stills doesn't work. I'm not getting the posts from my custom post type.
Any ideas how I could modify this? I am also still learning so being clear with explanations would be greatly appreciated.
Thank you!
Editing the pre_get_posts will replace the original query and you will not have the content for your page at all. I would only recommend going this approach if you only wanted to display the content of your portfolio post type and not the content of your portfolio page.
For general post queries it is recommended to use WP_Query or get_posts.
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Template_Tags/get_posts
If you use the WP_Query function the wp_reset_postdata() will restore the post data back to the original so you can get the content of your original page.
$args = array(
'posts_per_page' => 10,
'post_type' => 'portfolio',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Now you will be able to use the original loop to show the content of your page
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; // end of the loop. ?>
Usually, I stick my query posts in a variable, like so:
$catid = get_cat_ID('My Category Name');
$args = array(
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'category' => $catid
);
$posts_array = get_posts($args);
Then you can loop it like so:
<?php foreach ($posts_array as $post) : setup_postdata($post);?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Finally, to access your page content you can use the variable $post, it's automatically set by wordpress. No need to add any more code than this to access your page content.
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
The foreach loop for your page content is a little overkill, and there is a better way to do it (most likely at least), but I haven't been bothered to look into it further yet! It works though!