Wordpress: using query_posts with custom sort - wordpress

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.

Related

Get multiple categories names from permalink Wordpress

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.

How to change specific primary category name?

I have more than one category name in the post and I want to change specific primary category name to be normal category and then normal category to be primary category.
Let's say I have sponsored category and fashion category. So, I want to switch fashion category to be primary category because I do not want to show /sponsored/ in the URL.
Take Note: I have thousand of posts and I cannot switch the category name manually.
Using SQL and PHP you can run a foreach loop with all posts id that need to be updated.
A good starting point is an Excel file with all posts and its category ids that will be set as primary, so convert the spreadsheet to a json file to use in the PHP loop.
Below is the SQL query to do this:
UPDATE wp_postmeta SET meta_value = $new_primary_id WHERE post_id = $post_id AND meta_key = '_yoast_wpseo_primary_category';
Where $new_primary_id is the id of the category that will be set as primary, and $post_id
is the post id that will be updated.
If you using Yoast SEO Plugin there is the solution for you check here.
https://wordpress.org/support/topic/bulk-change-primary-category/

Can I get a specific number of posts per author and order by date in Wordpress?

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.

getting posts from multiple categories in blog category template

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.

Wordpress Archive by Year "query_posts()" Problem

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().

Resources