Not sorting alphabetically after submit the form - php

I have sorted dropdown menu with cities. It's sorted correctly, but after clicking the submit button, their order isn't alphabetical.
Query:
$cities = $wpdb->get_results( "select ID, post_title from $wpdb->posts where post_type = 'place' and post_parent = $state_id", ARRAY_A );

use bellow code hope it will help you-
$cities = $wpdb->get_results( "select ID, post_title from $wpdb->posts where post_type = 'place' and post_parent = $state_id ORDER BY post_title ASC", ARRAY_A );
for more https://codex.wordpress.org/Class_Reference/wpdb

Related

Select box, sort order of posts with custom fields and custom post type in Wordpress?

I want a select box that allows users to pick the order of posts displayed. (Like youtube order by date / relevance)
I'm displaying a list of custom post types (products). I want users to be able to select the order by price & size. (these are both custom fields).
I've written this code which allows me to change the order by changing the default variables below.
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $wpdb->postmeta.meta_value $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
I now need to let public change the default variables with a select box, but I don't know how.
<form method="post">
<select name="custom_field_choice">
<option value="size">Size</option>
<option value="price">Price</option>
</select>
<input type="submit" value="order_select" />
</form>
Is it possible to allow a user to change a php variable with a select box? If not, whats the best way to do this?
Try to change your code to receive variable from http post request like this
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
$orderField = $post->custom_field_choice;
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $orderField $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
Option's values size and price must be column name on your database table.

Wordpress Get Images using MySQL

I have an old function which uses the post excerpt to hold the image thumbnail. It was a bit hackey and worked for a long time.
Now I need to use the post excerpt for, you know, an excerpt. So I'm looking to update this function to grab the image src info straight from the post attachments instead.
The question:
How can I update the $before_sql SQL code below to grab the first attached image in the post attachment?
The code:
(I think I am only concerned about the sql portion as the rest should clean itself up?) There's more code section too, but instead of pasting ALL of it here, this snippet should be enough.
$before_sql = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' and post_date < '$cur_post_date' ORDER BY post_date DESC LIMIT $thumbnum";
$before_results = $wpdb->get_results($before_sql);
if($before_results) {
foreach ($before_results as $before_result) {
$post_title = stripslashes($before_result->post_title);
$permalink = get_permalink($before_result->ID);
$post_excerpt = ($before_result->post_excerpt);
$output="<div class=\"thumbnails\">" . $post_excerpt . "<br />‹</div>\n " . $output;
}
}
To get the first attachment with the posts in one query you can do in this way
SELECT *,
(SELECT guid FROM `wp_posts` WHERE post_type ='attachment' AND post_parent=wp.`ID` ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM `wp_posts` wp
ORDER BY post_date ASC will get the first image if you want the latest uploaded image you can simply use DESC ORDER BY post_date DESC
Here is your query
$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' ORDER BY wp.post_date DESC LIMIT $thumbnum";
It works fine for me
This is the query which will fetch those posts only which has the attachments on it
$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' HAVING attachment IS NOT NULL ORDER BY wp.post_date DESC LIMIT $thumbnum";

Custom select query wordpress order by meta key value

I am trying to ORDER BY my results of a custom select query.
But I am trying to order by the value of a meta key.
Please see my query below...
$get_atts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->posts.meta_key = 'surname' ASC");
As you can see this ORDER BY is breaking it...
ORDER BY $wpdb->posts.meta_key = 'surname' ASC"
So I am trying to order by the value of surname
But my does not seem to work. Can any explain why or help?
Try to use this query:
$get_atts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = 'surname'
AND $wpdb->posts.post_type IN ('individual') ORDER BY $wpdb->postmeta.meta_value ASC");

How to join wp_posts and wp_postmeta in wordpress ecommerce plugin retrieving only product title, product description and price?

I'm using wp-ecommerce plugin for my own online ordering bakeshop (http://www.kingsbakery.mpkproducts.com/)
I don't want the way how customers made an order. I want similar order form like this (https://www.ssfamousdeli.com/order_ship.aspx).
I run a custom query in Mysql and I can only retrieve the product title and description. I use this sql statement:
<?php
$sql = "SELECT * FROM wp_posts WHERE
post_type like 'wpsc_product' AND
post_status like 'publish'
ORDER BY ID Asc";
$query = mysql_query($sql);
?>
I only stop there coz I don't know how to join wp_postmeta to select the price.
Please help. Thanks
<?php
$sql = "SELECT wp_p.*, wp_pm.meta_value FROM wp_posts wp_p, wp_postmeta wp_pm WHERE
wp_p.post_type like 'wpsc_product' AND
wp_p.post_status like 'publish' AND
wp_p.ID = wp_pm.post_id
wp_pm.key = 'Price' --This should be the meta key
ORDER BY wp_p.ID Asc";
$query = mysql_query($sql);
?>
Hope this will be of help.

How can I grab data from all of the matching fields?

What is the propper syntax to grab data from all of the matching fields?
This example outputs only 1 of the matching fields:
$myvariable = "SELECT post_content
FROM wp_posts
WHERE post_name = 'testing'
AND post_status = 'publish'
AND post_type = 'post'";
echo = $myvariable;
You need to fetch an array of that query:
<?php
$query = mysql_query("SELECT post_content FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'");
while($result = mysql_fetch_array($query)){
echo $result['post_content'];
}
?>
That will loop through the result list from the query and echo the post_content field value.
EDIT: Wow... Same thing, a few seconds late. Ha!
Your question is not clear to me. There may be two cases:
You want to get all the rows which match you condition.
then you should use a loop to grab all matching records as below:
$result = mysql_query("SELECT post_content FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'");
if(mysql_num_rows($result)>0)
while($row = mysql_fetch_assoc($result)){
echo $row['post_content'];
}
I couldn't understand what you want to know. If you want to select all the fields of selecting row then use:
SELECT * FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'
OR
SELECT col1,col2,...,coln FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'
If you want to check conditions with all the fields then you are on the right track, just compare each value with its respective column.
list all the fields in your select statement or use * to select all.
$myvariable = "SELECT * FROM wp_posts WHERE post_name = 'testing' AND post_status = 'publish' AND post_type = 'post'"
You'll need to pull out the query and run a loop over the result rows that come back.
$query = mysql_query("SELECT post_content FROM wp_posts
WHERE post_name='testing'
AND post_status='publish' AND post_type = 'post'");
You'll need to adjust the above query to whichever rows you want to display as well as whatever criteria you want to match against.
And then run something like a while loop with each pass of the rows that were returned:
while($result = mysql_fetch_array($query))
{
echo $result["post_content"];
}
The above will go and pull out all the rows one by one that matched your query and display them however you style it.
If you want to match any, then your SQL is false.
SELECT post_content FROM wp_posts WHERE post_name = 'testing' OR post_status = 'publish' OR post_type = 'post'
The query above finds each record which contains exactly one of the search strings.
But if you want to find it within a sentence then you better use the MATCH function.
MySQL Full-Text Search Functions

Categories