I created an Archive list (also this is my archive.php codes) on my blog and i want to show posts by years.
But when i use query_posts() function for excluding some categories and limit posts then it's showing all posts not by year.
For example this page is showing all posts not only posts in 2009 years.
So if i summarize this issue; i want to show archive list by year (when i enter /2009/ permalink)+exclude some categories and limit posts.
Thank you.
maybe when you use global variable, $query_string, it will help.
so you must use it like this
global $query_string;
query_posts($query_string . '&cat=-13,-4,-14,-171&posts_per_page=5&paged='. $paged);
as codex mention, here : query post,
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().
Related
I am trying to get categories names for the ones that are available in permalink. e.g. I have the following permalink structure.
abc.com/product-category/category-1/category-2/
Where as category-1 and category-2 both are categories. When I try $wp_query->get_queried_object(), it just returns me info related to category-2. How can I get details of category-1 using any existing wordpress functions?
Thanks
As the previous categories are parent of the current use can get it using get_term https://developer.wordpress.org/reference/functions/get_term/
$child_category = $wp_query->get_queried_object();
$parent_category = get_term($child_category->parent, 'product-category');
You could get the parent of the category-2 using the get_term function.
$child_term = get_queried_object();
$parent_term = get_term($child_term->parent, 'product_cat'); // if your using Woocommerce then the taxonomy is called 'product_cat'
Both of these variables will be WP_Term objects.
https://developer.wordpress.org/reference/classes/wp_term/
From there you can access the name, id and any other information you need.
Revised
If you're wanting to create a custom category page (i.e. not a Woocommerce/WordPress page) then I would recommend adding a meta-box to the page editor.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://www.advancedcustomfields.com/
Have a select box with your product categories.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Then in your page template, use the term(s) ID as part of your WP_Query to pull products from both of those product categories.
I'm using
<?php query_posts('cat=34546&posts_per_page=5&order=DESC');
in order to get five posts from a certain category. The order is determined by the ID of the post. I would like it to be determined by another field, like post_date column in posts (let's say for the sake of this question that I manually change post_date that in the database). How can this be done?
Using the Query Posts orderby parameters.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
For example:
<?php query_posts('cat=34546&posts_per_page=5&order=DESC&orderby=date');
The reference I linked above has what values you can use for orderby.
I have a pretty basic query_posts call:
query_posts(array(cat=>3,author=>$userID,posts_per_page=>12))
This is getting 12 posts from the third category for a set of authors. The only piece I'm missing is I want those 12 to be made up of two posts each of my 6 authors. The order by date is fine, they don't need to be grouped by author.
Is there a way to pull posts this way?
I could be wrong, but I don't think this is possible with just one query since there isn't (to my knowledge) a way to specify how many posts per author.
It will add to your load time a little bit, but you might have to split it out into 6 different queries to get it to work.
You can do this by using WP_Query (your probably shouldn't use query_posts as it will change your main query) and wrapping each query in the specific author block:
$author1 = new WP_Query(array('cat'=>3,'author'=>$author1ID,'posts_per_page'=>12));
if($author1->have_posts):
while($author1->have_posts()):
$author1->the_post();
?>
<!--Insert HTML etc. here-->
<?php
endwhile;
endif;
wp_reset_query(); //reset the query object if running in the loop
Then do it for each of your other authors and that should do the trick.
Hope that helps. If there is a way to do one query I would definitely like to know since multiple queries can slow down page load.
I looking for a a way to use the exact same slug url for a number of posts.
The url structure for my site is:
domain.com/category/slug
If I have created a post with a slug called "martin-nicolausson" in category "february" i would like to be able to create the same slug for the next month without wordpress adding a unique identifier at the end.
For a live example of what I mean;
http://www.5070.se/february/jacob-astrom-2
Short answer: you can't do it.
Long version: Since you can change what category a page/post is in, WP requires that the slug be unique across all pages/posts. Think of a category as an attribute of the post, as opposed to defining a namespace in which posts exist. Because the true namespace is global and flat, the slugs must be unique.
I have 5 blog sections each representing a category. However, I would like to have a category called "all" that if checked would include the post in all 5 section without having to click every category.
I guess the only way to do this is to add something into the blog category template file to tell it to include the usual category posts as well as the "all" category.
now I think i've located where it queries the post:
if ( $wp_query->have_posts()) : while (have_posts()) : the_post();
but there is nothing identifying the current category id, except for a few things above for specifying the right title for the theme.
What would be the best way of achieving what I have explained?
You can interact with the wp_query object... the same way that you interact with query_posts()... it accepts the same arguments.
For either you can use Category Parameters as follows (from the wordpress codex: query_posts function reference)
Category Parameters
Show posts associated with certain categories.
cat (int) - use category id.
category_name (string) - use category
slug (NOT name).
category__and (array) - use category
id.
category__in (array) - use category
id.
category__not_in (array) - use
category id.
When it references $wp_query, the variables of the query have already been defined, you can set up your own query with any parameteres you want.