Custom post type date output - wordpress

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.

Related

How to use Wordpress Trim Words Function

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];

Wordpress - Divide average by 2 decimal places

I have code that gives me the average of certain number which is fine however, it's giving me quite a long number e.g. 4.8571428571429
Is it possible to get it down to 2 decimal places e.g. 4.85
Hope it makes sense, my code so far is below
<?php // Get total number of posts in custom post type
$count_testimonials = wp_count_posts('testimonial');
$total_testimonials = $count_testimonials->publish;
$new_average = ($add) / ($total_testimonials);
echo $new_average;
?>
Try to use this: number_format((float)$new_average, 2, '.', '')
<?php
$count_testimonials = wp_count_posts('testimonial');
$total_testimonials = $count_testimonials->publish;
$new_average = ($add) / ($total_testimonials);
$new_average;
echo number_format((float)$new_average, 2, '.', '');
?>
Hope this will help you. Thanks.

Sort posts by custom field - numbers and string

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

Query is ordered correctly in SQL but not in Drupal

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.

mysql_fetch_row loop?

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.

Resources