I hope someone can help me with this. I have been reading other posts, the Codex, and trying others code, but can't fix my query.
I am creating a Page that contains a list of authors. The authors are in two categories, and I need to sort by last name, first name. I want to sort them by the custom field wpcf-sortname (from the Types plugin).
I get the correct results from the query, but the results are sorted by ID.
Note: I'm not very good with queries, but any help will be appreciated.
I've tried:
<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>meta_value,'order'=>ASC,));
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile; // end of the loop. ?>
And:
<?php
$args = array( 'category__and'=>array(48,49),'meta_key'=>wpcf-sortname,'orderby'=>wpcf-sortname,'order'=>ASC,'posts_per_page'=>-1);
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<div>
<?php the_title(); ?><br />
</div>
Try with this
<?php query_posts(array('category__and'=>array(48,49),'meta_key'=>'wpcf-sortname','orderby'=>'meta_value','order'=>ASC,));if ( have_posts() ) while ( have_posts() ) : the_post(); ?><?php the_title(); ?><br /><?php endwhile; // end of the loop. ?>
I have just put the single quote in metakey and orderby field. I think this will work for you.
Related
I need to get second newest post from wordpress, and I cant do this. I found I lot of tutorials how to get several newest posts, but no how to get only one post that is second in the order (or fifth, or tenth).
I have something like this:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(get_posts()[1] == true) : ?>
<?php get_template_part( 'content-second-row', ( post_type_supports( get_post_type(), 'post-formats' ) ? get_post_format() : get_post_type() ) ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
But, of course it is not working. I tried to get get_post().length. I know, unfortunately I can't use posts id, because there is no one by one.
Please, help me.
Of course, I try to finde something in wordpress codex
try this
get_posts( array('posts_per_page' => 5,'offset' => 1,))
You can change offset to any number you want.
I'm using a theme for some Wordpress-integrated software which means that I am basically stuck with the template hierarchy, otherwise I'd just set up a custom template to get around this.
Anyway, I have my page.php, which looks a little like the following
<?php //start the main loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
if ( is_page('contact-us') ) {
get_template_part( 'content', 'contact' );
}else{
get_template_part( 'content', 'page' );
}
?>
<?php //end the main loop
endwhile;
else:
?>
Something is missing
<?php
endif;
?>
This works fine, and as expected, I am able to add html within content-page.php
However, I would like to add a custom loop within content-page.php to display customer testimonials. I've attempted this with the code below inside content-page.php:
<?php //close the main loop
endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
//close the main loop and open a custom loop
wp_reset_postdata();
$args = array (
'post_type' => 'testimonial',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$the_query = new WP_Query ( $args );
if ( have_posts() ) : while ( $the_query ->have_posts() ) : $the_query ->the_post(); ?>
Do HTML stuff here
<?php //close the custom loop
endwhile; else: ?>
Uh oh, there is meant to be a testimonial here. Please create some.
<?php endif; ?>
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
This code creates a PHP error (unexpected endwhile where I attempt to close the main loop). However, I put this exact same code straight inside page.php, it works. It only errors when inside content-page.php. Am I not able to include custom loops with get_template_part?
the last endwhile and endif are not closed
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
endwhile;
endif;
?>
How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>
I am getting horribly frustrated trying to build multiple loops in WordPress. I've looked at loads of articles - what am I doing wrong. I placed the following in the loop.php file (because I've built the homepage on this)...
<!--Loop 1-->
<?php global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php global $query_string2; // required
$posts = query_posts($query_string2.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
// your output html
<?php endwhile; ?>
You have nested the loops (missing endwhile).
You cannot just invent a variable "query_string2" and expect it to work ;)
Try this:
<!--Loop 1-->
<?php
global $query_string; // required
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
If that doesn't work (I don't remember whether wp_reset_query() resets the $query_string global off the top of my head), try the following:
<?php
global $query_string; // required
$query_string_backup = $query_string;
$posts = query_posts($query_string.'category_name=news&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
<!--Loop 2-->
<?php wp_reset_query(); // reset the query ?>
<?php
$posts = query_posts($query_string_backup.'category_name=jobs&posts_per_page=3');?>
<?php while ( have_posts() ) : the_post(); ?>
This will work:
<?php
global $query_string; //make sure these are in the correct format for post queries
global $query_string1;
// First loop
$first_loop = new WP_Query( $query_string.'category_name=news&posts_per_page=3');
while ( $first_loop->have_posts() ) : $first_loop->the_post() :
// do your thing
// e.g: $first_loop->the_content();
endwhile;
// second loop
$second_loop = new WP_Query( $query_string1.'category_name=news&posts_per_page=3');
while ( $second_loop->have_posts() ) : $second_loop->the_post() :
//do your thing
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I could help out, but I think that first, I should understand 1 fundamental: WHAT are you trying to do exactly? What is your goal? Why do you think you need nested loops?
This will help me aswering more correctly.
I solved it with the following code, which I put in a page template (my friend advised that it's best leaving the loop.php alone)
<?php $custom_query = new WP_Query('category_name=news'); // only News category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php $custom_query = new WP_Query('category_name=jobs'); // only Jobs category
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content();?>
<?php endwhile; ?>
There's a function called rewind_posts() that rewinds the loop posts in order to let you re-use the same query in different locations on the same page
https://codex.wordpress.org/Function_Reference/rewind_posts
I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.