Get all Custom posts beginning with letter A - wordpress

I have a custom post type(event) with a custom taxonomies(event_countries).
http://educationgate.org/en/event_countries/united-arab-emirates/
I want to get all the custom posts that bening with the letter A.
The issue is i want to get this through a url string.
I have tried http://educationgate.org/en/event_countries/united-arab-emirates?orderby=title&order=ASC&s=A
But this is going to the search page and show all posts.
Can somebody help?

This must be helpful
global $wpdb;
$request = "A"
$results = $wpdb->get_results(
"
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$request%'
AND post_type = 'event'
AND post_status = 'publish';
"
);

Related

Woocommerce: order by custom taxonomy

I'm having the following problem. I have set up a Wordpress site with WooCommerce to serve a webshop with only books.
I have created some product attributes that are based on taxonomies like 'publisher' and 'author' (as multiple products can share an author or a publisher)
I would like to be able to sort my products not only on the Woocommerce default fields like 'title' and 'price' but also on these taxonomy fields.
Say for example:
order by Author ASC or order by publisher DESC
As far as I have discovered there is no way to do this with Wordpress core functions. Some say this is because it makes no sense to sort by taxonomy fields, but with the above example I can't understand why you don't want to sort by for example author.
I have played around with sortby meta_value, but this is just querying the direct postmeta, not the taxonomies.
I have knowledge of php, so any solutions involving additional code in my functions.php file will do.
I found out a way how to solve this:
https://gist.github.com/jayarnielsen/12f3a586900aa6759639
I slightly modified the code so it equals the way Woocommerce uses to sort by the system fields "title" and "price" which is adding a url query-param named "sortby" a value structured like this: "field"-"direction"
So to sort by a product property called "pa_tax1" ascending you'll need to add to the url: sortby=pa_tax1-asc
add_filter('posts_clauses', 'posts_clauses_with_tax', 10, 2);
function posts_clauses_with_tax( $clauses, $wp_query ) {
global $wpdb;
// Array of taxonomies you want to sort on
// in case of Woocommerce Properties, be sure to add the pa_ prefix
$taxonomies = array('pa_tax1', 'pa_tax2', 'pa_tax3');
// If no orderby query param is found, do nothing
if( !isset($wp_query->query['orderby']) ) {
return $clauses;
}
// Explode the orderby query-param
$orderQuery = explode('-', $wp_query->query['orderby']);
$orderBy = [];
$orderBy['field'] = $orderQuery[0];
$orderBy['direction'] = (isset($orderQuery[1])) ? strtoupper($orderQuery[1]) : 'ASC';
// Only add clauses, if the sortby field is in our array
if( in_array($orderBy['field'], $taxonomies) ) {
$clauses['join'] .= "
LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
";
$clauses['where'] .= " AND (taxonomy = '".$orderBy['field']."' OR taxonomy IS NULL)";
$clauses['groupby'] = "rel2.object_id";
$clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
$clauses['orderby'] .= ( 'ASC' == strtoupper( $orderBy['direction'] ) ) ? 'ASC' : 'DESC';
return $clauses;
}
else {
return $clauses;
}
}

Add Custom Function to WooCommerce

Two questions here.
Below is a function in WooCommerce wc-order-functions.php that I found works almost similar to what I need.
function wc_get_order_id_by_order_key( $order_key ) {
global $wpdb;
// Faster than get_posts()
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND meta_value = %s", $order_key ) );
return $order_id;
}
Which returns the $order_id but I'm looking to get the $order_key so that I can contruct the url generated after buyer have click the 'Place order' button on the checkout page e.g. domain.com/checkout/order-received/2316/?key=wc_order_54c7142660e24
wc_order_54c7142660e24 being the $order_id and
2316 being the $order_key
I know that the SQL command below gets the value I want from database:
function wc_get_order_key_by_order_id( $order_id ) {
global $wpdb;
// Faster than get_posts()
$order_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND post_id = %s", $order_id ) );
return $order_key;
}
But I'm not sure how to hook the filter to wordpress functions.php So this is my first question.
Second question is I've been searching for the function that might be responsible for generating the meta_value for the $order_key but can't seem to find it, would be great to know how it works. I suppose the first way is kinda redundant since the function I'm looking for already exists, just can't seem to locate it.
Try adding this to your woocommerce template files where you need that link:
echo get_site_url().'/checkout/order-received/'.$order->id.'&key='.$order->order_key;

wordpress get post_id where meta_value =

I am trying to query the wordpress post_meta table by meta_value.
I would like to output all post_id's where the meta_value is = to _parent_product. Here is my code:
$posts = $wpdb->get_results("SELECT *, FROM $table WHERE meta_key='_parent_product' ");
foreach ( $posts as $post ){
$id = $post->post_id;
echo $id;
}
The above outputs nothing and im not quite sure why? Can anyone see anything wrong?
As stated in the comment,
There is a comma (,) behind SELECT *,. Therefor the given SQL is invalid and will fail to retrieve any results.

sort wordpress posts by title, ignore articles like “the”, “a”, “an”

I'm sorting my posts alphabetically by Title, like so:
<?php
{
$posts = get_posts($query_string .
'&orderby=title&order=asc&posts_per_page=-1');
}
get_template_part( 'loop', 'category' );
?>
I'd like to exclude articles such as "the", "a", and "an" from the sort.
What would be the best way to accomplish this?
Thanks!
I don't know any simple way to do that but you can do this,
For achieving this you need to add a custom meta field to the post. Name it mytitle (say).
For the new posts you add, it is simple, you have to add your modified title(removing a, an, the from the title) in the mytitle custom field in the add posts page.
For old posts it is a bit tricky, you have to write a php code to retrieve the titles of the post remove 'a','an','the' from them using php preg_replace and add it to the postmeta table of your wordpress database using something like this:
<?php //inside loop
$query=INSERT INTO xyz_postmeta (post_id, meta_key, meta_value) VALUES ($postid, 'mytitle' $title);
$wpdb->query('$query'); ?>
where $postid is the post id inside the loop and $title is your modified title.
Now you have updated all the previous posts with custom mytitle field.
Now to display, you have to use a custom loop (not the loop included in the theme).
Here is how you can make a basic custom loop to display posts sorted in order of mytitle.
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'mytitle'
AND wposts.post_type = 'post'
AND wposts.post_status = 'publish'
ORDER BY wpostmeta.meta_value ASC
";
Now you can execute the query by any means you want. Wordpres provides various methods to do so. Here's a link
For example you can do something like this
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ( $pageposts as $pagepost )
{
echo $pagepost->post_title;
//do other stuff to display content, meta etc..
}
You can actually achieve this by manipulating the ORDERBY clause using the posts_orderby_request filter, and using TRIM to ignore articles "the", "a", and "an". Here's an example:
<?php
add_filter( 'posts_orderby_request', 'myproject_title_sort', 10, 2 );
/**
* Ignore opening articles when sorting by Title.
*
* #param string $orderby Order by parameter.
* #param WP_Query $query WP Query object.
*
* #return string
*/
function myproject_title_sort( $orderby, $query ) {
global $wpdb;
// Be sure to add a condition that matches your criteria (post type, archive, etc).
// In this example, we bail early if it's an Admin query, or not a main query.
if ( is_admin() || ! $query->is_main_query() ) {
return $orderby;
}
// This is another check to see if we're on a particular post type archive.
if ( ! $query->is_post_type_archive( 'my-post-type' ) ) {
return $orderby;
}
$title_col = $wpdb->posts . '.post_title';
// Check if we are sorting by post_title.
// You may need to use a separate `pre_get_posts` filter to sort by "title" if not already doing so.
if ( false === strpos( $orderby, $title_col ) ) {
return $orderby;
}
$ignore = "TRIM( LEADING 'the ' FROM LOWER( TRIM( LEADING 'a ' FROM LOWER( TRIM( LEADING 'an ' FROM LOWER( $title_col ) ) ) ) ) )";
$orderby = str_replace( $title_col, $ignore, $orderby );
return $orderby;
}

wordpress num rows, how?

I'm creating a plugin for wordpress and I need to check if a post with a specific title exist or not. This to prevent duplicates, how can I query this and see if it returned true or false?
I’m using this code to get the ID of a post/page by title:
function get_id_by_name($name)
{
global $wpdb;
$id = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_title = '$name'
LIMIT 1");
return empty ( $id ) ? -1: (int) $id[0];
}
If the post doesn’t exists, the function return -1.

Resources