Wordpress: WP_Query search criteria on 'post_name' - php

I'm using WP_Query (pretty standard). It all works great.
However, I have a particular modification to make, where, if the user enters the specific post name in the URL, the search will return only the post that matches that post_name value.
See my code below with a comment about the particular line not working.
<?php
$getPeople = array(
'post_type' => 'person',
'posts_per_page' => -1,
// I want this below to only return me the post with this specific value.
// This doesn't error, but doesn't work either.
// I know it seems counter-productive to a 'search' but this particular case requires it.
// This has a hard-coded value at the moment.
'post_name' => 'rebecca-atkinson',
'orderby' => 'meta_value',
'meta_key' => 'last-name',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'gender',
'value' => $theGender,
)
),
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'accent',
'field' => 'slug',
'terms' => $theAccent,
'operator' => 'IN',
),
array(
'taxonomy' => 'style',
'field' => 'slug',
'terms' => $theStyle,
'operator' => 'IN',
),
array(
'taxonomy' => 'age',
'field' => 'slug',
'terms' => $theAge,
'operator' => 'IN',
),
)
);
$myposts = new WP_Query($getPeople);
?>
Your help would be greatly appreciated. If I could just see how to search on this specific 'slug' then that would be great.
Many thanks,
Michael.

Instead of
'post_name' => 'rebecca-atkinson',
use:
'name' => 'rebecca-atkinson',

In addition to my answer in the comments above, I thought I'd post it as an official answer too:
I have to use 'name' and NOT 'post_name'.
For example:
'name' => 'rebecca-atkinson'
Hope this helps someone in future.

Related

Custom related posts with product attributes

I try to get custom related posts for my woocommerce product page based on its attributes.
I set up the following php query, to find the products with the exact same attributes:
$pa_farbe = $attributes["pa_farbe"];
$pa_material = $attributes["pa_material"];
$pa_steine = $attributes["pa_steine"];
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $parent_cat,
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => '_price',
'value' => array($price_min, $price_max),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_farbe',
'field' => 'slug',
'terms' => $pa_farbe->get_slugs(),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_material',
'field' => 'slug',
'terms' => $pa_material->get_slugs(),
'operator' => 'IN',
),
array(
'taxonomy' => 'pa_steine',
'field' => 'slug',
'terms' => $pa_steine->get_slugs(),
'operator' => 'IN',
)
),
'exclude' => array( $product_id )
));
However, not all products have an attribute "pa_farbe" assigned. So for those products the php code shows an error on the frontend.
How can I check before, if the attribute is available for the current product?
I tried getting the attributes with this code:
foreach( $product->get_attributes() as $attr_name => $attr ){
echo wc_attribute_taxonomy_name( $attr_name );
}
but I don't know how to use an if query in the get_posts function.
I'm not an experienced developer so in my head this would be a solution that works but is not allowed in get_posts:
if (!sizeof($pa_farbe)=0) {
array(
'taxonomy' => 'pa_farbe',
'field' => 'slug',
'terms' => $pa_farbe->get_slugs(),
'operator' => 'IN',
),
}
Ideally the 'tax_query' => array() is filled with the current products attributes, could someone help me out here?

Sorting query ASC giving wrong order

I am creating a simple Query that should query through a meta value containing a number. 1 should be first, 2 should be 2nd and 3 should be 3rd.
For some reason, it comes out like 1, 3, 2 in my query. What am I missing??
$args = array(
'post_type' => 'x-portfolio',
'posts_per_page' => $count,
'paged' => $paged,
'orderBy' => 'meta_value_num',
'meta_key' => 'liste_nr',
'order' => 'asc',
'tax_query' => array(
array(
'taxonomy' => 'portfolio-category',
'field' => 'term_id',
'terms' => $filters,
),
array(
'taxonomy' => 'portfolio-category',
'field' => 'name',
'terms' => 'Accessories',
'operator' => 'NOT IN'
)
)
);
I've run into this before when a CPT, plugin etc has done a custom query and they never reset and it can completely override yours.
Try dropping wp_reset_query() before it and make sure you are using it after.
https://developer.wordpress.org/reference/functions/wp_reset_query/

orderby and order filter in get_posts or WP_query function in wordpress not working

I have function in wordpress plugin which queries the posts using get_posts($array). But I wanted this to orderby post_modified field of the posts table in DESCENDING order, for which I have this code below:
$arrPostDtls = get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft') ,
'orderby' => 'post_modified',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Here, I did implemented the orderby or order clauses to get it sorted accordingly, but it doesnt work. Please suggest or help me to get the sorting as I am willing to.
UPDATE To get the things other way, I used the WP_query method to get the posts. for which I implemented below code:
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => 'modified',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
From this I recieved the result which also contains the SQL query, and executing the SQL query in PHPmyadmin, I found the exepected result, but when i iterated the "$arrPostDtls->posts", it still gives me the old results.. Please suggest what is wrong here..
Try
get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending',
'trash','draft','auto-draft') ,
'orderby' => array('post_modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
Update after you update the question
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => array('modified' => 'DESC'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
I found my problem on the UI side (thanks to #Zhilevan) where by ajax response was being exposed to a jQuery library DataTable(), whose default ordering was sorting in alphabetical order. I set the ordering parameter to false as:
$("#someid").DataTable({"ordering":false});
And My results were displayed as I was willing to

Wordpress tax_query "and" operator not functioning as expected

I have a custom post type of image with a custom taxonomy called image_tag (it's hierarchical like categories). Here are some examples of the tags that might be used:
Structure (id: 25)
- House (id: 56)
- Skyscraper
Nature
- Animal
- Plant (id: 41)
So, I want to drill down through the images by selecting multiple tags in conjunction with the "and" operator. For example, finding all photos with plants and houses.
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(41, 56), // IDs of "plant" and "house"
'operator' => 'and',
),
),
);
That works fine, the problem begins when I try to include the parent terms, for example:
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'and',
),
),
);
Then I get no results. I'm guessing that because I'm using the "and" operator, Wordpress doesn't include the children of the "Structure" term. Does anyone have an idea how I can get this to work?
So it should look like this:
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 25 ),
),
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 41 ),
),
),
Update: You forget to close 'terms' and 'operator' by ' like this
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'in'
),
),
);
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'in'
),
),
);
I had a similar problem, give this a try!

Query posts from two post formats within a category

I have been reading the WP_Query Codex looking for a way to loop through all the posts that have the post format 'video' OR 'image', within a given category.
If this wasn't enough, this category is given by a variable $catslug (I need it to be this way).
I have only found ways of looping through one of the following
image OR video
image AND category
video AND category,
but what I need is more complex, something like this:
post-format-image AND $catslug) OR (post-format-video AND $catslug)
Is it possible to do a tax_query within a tax_query?
Something like this:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-image' )
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($catslug)
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array( 'post-format-video' )
)
)
)
);
$query = new WP_Query( $args );
Anyone knows any workaround or a hack to get this?
Perhaps I'm just thinking the wrong way.
This actually a good question. The simple answer here is that you cannot use multiple tax_query's.
This had me quickly testing the following scenario before I left for work. Just for fun I went and tried to make use of the category parameters with a tax_query, but that gave me posts from the desired category and posts that also belongs to both post formats
I came up with a possible solution, unfortunately I cannot test this right now.
Here is my line of though here:
As you need random results, I would suggest that you add your post formats into an array
$post_format_array = array( 'post-format-video', 'post-format-image' );
You now are going to use shuffle to shuffle the array, and then take the first entry from that array, which will be either video of image, and use that in your tax_query.
shuffle( $post_format_array );
This way you will get posts that is in your desired category and in either video or image post format.
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $catslug,
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_format_array[0]
),
),
);
$query = new WP_Query( $args );

Categories