I've a weird problem, some posts appears in categories where they are not in.
When I look in my backoffice and filter by categories, some post appears there but they are not checked in.
The resultat is that in the front office they appear too.
This is my category.php (but I don't think it's the matter)
<?php
get_header();
?>
<section id="wrapper" class="page <?php echo get_query_var('cat'); ?>">
<div id="container">
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
query_posts('showposts=1&cat='.$cat_id);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
</div>
</section>
<?php
get_footer();
?>
I looked in the table "_term_relationships" and everything is right, they're not in the wrong categories.
So maybe someone have a clue to find out ?
PS : I'm using WPML, but if I desactive it, it's the same problem
You should not use query_posts(),
see (https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)
try this:
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
$args = array( 'category' => $cat_id );
$query2 = new WP_Query($args);
if ( $query2->have_posts() ) :
while ( $query2->have_posts() ) :
$query2->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
First of all, never use query_posts to construct any type of query
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Secondly, never change the the main query for a custom query on any type of archive page or home page. The correct way is to use pre_get_posts to alter the query variables before the main query executes. Check out this post I've done a while ago
Thirdly, category pages in Wordpress does work in a strange way. When a category page is visited, it will display posts from the selected category and posts from the selected category's child categories. I bet this is what you are seeing. This is pretty normal behavior. If you need to change this, have a look at this answer on WPSE by #ialocin. For the benefit of this answer, here is the solution
add_filter(
'parse_tax_query',
'wpse163572_do_not_include_children_in_category_archive_parse_tax_query'
);
function wpse163572_do_not_include_children_in_category_archive_parse_tax_query( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_category()
) {
// as seen here: https://wordpress.stackexchange.com/a/140952/22534
$query->tax_query->queries[0]['include_children'] = 0;
}
}
Related
I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
I have spent some time reading through the WordPress Codex, as well as various themes, and can see that some developers insert <?php wp_reset_postdata(); ?> after the endif; in a Blog Loop whilst others insert the code between the endwhile; and endif; of a Blog Loop. I have tried both locations but have yet to see a difference. Is there a correct location?
This function supposed to reset the secondery query that you run.. the function the_postis letting you to use all the functions that you can run in the loop like the_title() the_content and so on..
So you reset the the_post function and after the endwhile; you can reset it already. and use your main query inside the if statment too if you like.
<?php
// this is the main query check if there is posts
if ( have_posts() ) :
// loop the main query post and run the_post() function on each post that you can use the function the_title() and so on..
while ( have_posts() ) : the_post();
the_title(); // title of the main query
// the is second query
$args = array( 'posts_per_page' => 3 );
$the_query = new WP_Query( $args );
// check if there is a posts in the second query
if ( $the_query->have_posts() ) :
// run the_post on the second query now you can use the functions..
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
endwhile;
// reset the second query
wp_reset_postdata();
/* here we can use the main query function already if we want..
its in case that you want to do something only if the second query have posts.
You can run here third query too and so on...
*/
the_title(); // title of the main query for example
endif;
endwhile;
endif;
?>
wp_reset_postdata() is only required if you have a secondary loop (you're running additional queries on a page). The purpose of the function is to restore the global post variable back to the current post in the main query.
Example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
OUTPUT MAIN POSTS HERE
<?php endwhile; endif; ?>
In the code above I'm simply using the main loop to display posts. There's no reason to include wp_reset_postdata(). The global post variable is exactly what it's supposed to be.
If somewhere else on the page I decide to add a secondary loop, then I'll need wp_reset_postdata().
// This query is just an example. It works but isn't particularly useful.
$secondary_query = new WP_Query( array(
'post_type' => 'page'
) );
if ( $secondary_query->have_posts() ) :
// Separating if and while to make answer clearer.
while ( $secondary_query->have_posts() ) : $secondary_query->the_post();
// OUTPUT MORE STUFF HERE.
endwhile;
wp_reset_postdata();
endif;
To answer your original question: it usually comes after endwhile and before endif. It's the call to the_post() which actually changes the global post variable. If there are no posts then the post variable remain the same and there's no reason to use the reset function.
It's been a while since I've worked with PHP and WordPress, so I am a bit rusty. What I want to do is to display the opening paragraph for the most recent post under a category for a WordPress site. Based on some research I did, I've compiled this piece of code:
<?php
$category_id = get_cat_ID('Downtown News');
$post = get_posts( $category_id );
if( !empty( $post ) ) {
setup_postdata( $post );
?><?php the_title(); ?>
<?php the_excerpt();
} ?>
<?php
$post = $wp_query->post;
setup_postdata( $post );
?>
I have code to get the category ID I want, and I have the code to display the first paragraph of the most recent article. However, the code does not seem to work. What is displayed is the first paragraph under an "uncategorized" category of postings, which is not what I want. How can I fix what I have so that it gets the correct category?
You have started at the right place: get_posts, however you don't have the correct parameters. So try the following out:
<?php
$args = array(
'category' => 'CAT_ID_1,CAT_ID_2',
'orderby' => 'post_date',
'order' => 'DESC');
$posts_array = get_posts( $args );
?>
From the function reference we know that:
The category parameter needs to be the ID of the category, and not the
category name
which means that you can have one or more category IDs (comma separated).
A list of all parameters can be found here.
I had the similar issue with yours, maybe this one will work for you
notice the $category_id gets pulled from get_cat_ID() function, this tells me you do not know the cat_id, you can go to the category where you created the downtown news, move your mouse over it to reveal the url address which it will tell you the category id, find that number and replace the line:
query_posts('cat='.$category_id.'&posts_per_page=1');
(using 99 as an example)
query_posts('cat=99&posts_per_page=1');
<?php
global $post;
$category_id = get_cat_ID('Downtown News');
query_posts('cat='.$category_id.'&posts_per_page=1');
if ( have_posts() ) {
?>
<?php the_title(); ?>
<?php the_excerpt();
}
wp_reset_query();
?>
I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.
I've got post type called "Portfolio" and single-portfolio.php file to handle it (it's WordPress). When I use there something like that it works like expected:
$post_id = $post->ID; //returns ID of current portfolio post. Good!
BUT when I post short query like this in the middle:
$post_id = $post->ID; //returns ID of current portfolio post. Good!
wp_reset_query();
query_posts('posts_per_page=4');
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_id(); //returns ID of standard blog post
endwhile;
endif;
wp_reset_query();
$post_id = $post->ID; //returns ID of last BLOG post. Wrong!
I'm only concerned about $post_id variable in above example. I want it to always return correct ID of current PORTFOLIO post and not be dependent on other queries. How do I achieve that?
I believe wp_reset_postdata() will give you the result you are looking for.
$the_query = new WP_Query( 'posts_per_page=4' );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// output
endwhile;
endif;
wp_reset_postdata();
I should note that there is another approach which I have documented in another question asking about what is the difference and when each should be used.
The wp_reset_query function does reset the global $post variable as well, but only based on the global $wp_query variable. That still is modified, probably due to one of the little flaws in Wordpress. In your case I'd say a simple WP_Query::rewind_posts() should do it:
wp_reset_query();
$wp_query->rewind_posts();
$post_id = $post->ID;
Also you should consider to create a second loop, not overwrite the first one.
See as well:
Wordpress wp_reset_query() does not go back to last query?