So i have a custom field that gives output as plain text in this format: dd. MM yy (09. October 2013). This is the loop that im using:
query_posts(array('category_name'=>'somecategory', 'posts_per_page'=>'5','paged' => get_query_var('paged')));
while (have_posts()) : the_post();
//stuff
the_field('mydate'); //the output is: dd. MM yy
//stuff
endwhile;
wp_reset_query();
//paginate_links code...
If i add this to query_posts:
'meta_key' => 'mydate', 'orderby'=>'mydate'
My posts get sorted only by day and year couse it seems it cant read the month couse its a string. And this kind of sorting is kinda usless. If the site was my i would change date format and it would be solved, but i am doing this site for someone and the designer already draw this date format in photoshop so it has to be this way. So how can i make this sort work properly?
Add below code before you query_posts:
add_filter('posts_where', 'filter_orderby');
function filter_orderby( $where = '' ) {
$where .= " AND $wpdb->postmeta.meta_key = 'mydate' ORDER BY mydate DESC";
return where;
}
hope it helps
Related
i want to trim a single word i.e, Monday in wordpress, how can i trim this word?
$my_title = get_the_title();
echo wp_trim_words($my_title, 1, null );
the title coming from database Monday So i want to trim Monday to Mo or M.
wp_trim_words() will works easily without any problems. You can use it with below sample code:
echo wp_trim_words( get_the_title(), 1, '' );
But do note that it will trim the first character in whole post/page title, not regconize the date in your example.
I don't know that wp_trim_words() works that way. I'd recommend just using regex and preg_match():
$my_title = get_the_title();
to get the first character:
$regex = '/(.?)/';
or to get the first two characters, change the variable to this:
$regex = '/(.?)./';
Then get the matches for the regex pattern:
preg_match($regex, $my_title, $matches);
Echo out the first one:
echo $matches[0];
I have created a custom post type for contests.
and 1 of the fields is a closing date , everything is going fine only i cant figure out how to output the date format on the way i want it.
with this code in my loop i get the date , but not as ( day-Month-year )
<?php echo get_post_meta($post->ID, "_closingdate", array("format" => 'd m, Y'), true); ?>
the output remains year-month-day
i have searched everywhere to find a solution but i cant find it or i am overlooking something
Do this
echo date('d m, Y', strtotime(get_post_meta($post->ID, "_closingdate")));
i would do
echo date('d m, Y', get_post_meta($post->ID, "_closingdate"));
i dont know what type you used for this, but if timestamp from time() then its ready to use. If not - then you might have to parse it as string to date 1st then parse it to date.
I have a query that takes a string 'noticeDate' and converts it to a date to order it by. The query works perfectly when I run it in phpmyadmin but not in drupal
Here's my query
<h3 style="text-align: center;">Public Notices</h3>
<?php
$query="SELECT `noticeTitle`, `noticeDate`, `filename`, STR_TO_DATE(`noticeDate`, '%m/%d/%Y') as filedate, `filepath`
FROM `public_notice`
ORDER BY filedate DESC
LIMIT 5";
$results = db_query($query);
while ($row = db_fetch_array($results)) {
echo '<tr>';
echo '<td><u><a href=http://website.com/'. $row["filepath"] . '>' .$row["noticeDate"].'</u>- '.$row["noticeTitle"]. '</a></td><br />';
echo '</tr>';
}
?>
In phpmyadmin, it orders 5 dates perfectly- but in drupal it looks like this.
08/04/2009- sometext
09/14/2009- sometext
01/28/2009- sometext
01/23/2009- sometext
02/25/2009- sometext
I think the problem is with the way Drupal substitutes arguments / named parameters. See the second parameter for db_query.
You should really refactor your code to use a db_select. https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_query/7
https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_select/7
To add the STR_TO_DATE functionality, use the AddExpression functionality. https://api.drupal.org/api/drupal/includes!database!select.inc/function/SelectQuery%3A%3AaddExpression/7
Using these two will definitely solve your problem. Good luck.
I am trying to learn how to create my own module and I am stuck. I created text.module, text.install and text.info . It's just a basic module for inputing text.
So now I have the next scenario I input the text in textbox, it saves to a table in the database i can display it into a table like in this:
http://img33.imageshack.us/img33/5815/textyi.jpg
now my question is can you and how can you use function view so that in the picture "ttt" becomes a link so that you can see the title and text "ttt" ?
EDIT :
I figure out something i just need this :
in table I have 3 columns (test_id, title, text)
Now i use
$result = db_query("select title,text from {text} ");
now how can I turn title into a link . Let say that in row 1 col1 of title it stands house. How can i turn house into a link. Do i need to do sometning like this
$result= db_query ("select n.title, n.text form {text} n where n.title = :title", array (":title" => ???? ))
I think this is what you are looking for
<?php
l(t('Link text'), 'about-us', array('attributes' => array('class' => 'about-link')));
?>
http://api.drupal.org/api/drupal/includes%21common.inc/function/l/7 might help u..
I set a session variable upon login, I want to find all rows in a table that have the username in the "Createdby" field and I want to list them on a page. I'm using this code:
<?php
$result = mysql_query("SELECT email FROM members WHERE createdby = '" . $_SESSION['myusername'] ."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result))
{
echo ($row[0]);
}
?>
It works great but it doesn't space them, it echoes out like: data1data2 and not separate like data1, data2. How can I customize the results without messing it up? I tried to add
echo ("<p>".$row[0]."</p>");
But received: 11, I'm kind of new to PHP.
This is simply string concatenation. You're adding strings to other strings.
I'd suggest the following just to get started:
echo $row[0].", ";
I also suggest you read up on PHP more, as this is a basic concept in the language.
You can add spaces between each $row[0] like this:
echo $row[0] . ' ';
The dot followed by a space means that you're concatenating a space.