wordpress query all post that start with number - php

I am trying to show all posts that contain a number.
I am using this code :
global $wpdb;
$postids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",REGEXP ^[0-9]));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'shows',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $_GET['letter'];
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
}
I have tried this but it is not working it just 1 post, but when i change it to show for example posts that start with a it show all of them.
RLIKE ^[0-9] [0-9]%
Thanks.

To fetch only records starting with number, filter them using REGEXP
SELECT ID
FROM $wpdb->posts
WHERE $wpdb->posts.post_title REGEXP '^[0-9]+'
ORDER BY $wpdb->posts.post_title;

Related

Wordpress Get Category From Post Table

I am trying to display last couple of post title in a comma separated value with below code.Here I am making the query from wp_post table.Now How can I get the category in this code as there is no column for category in wp_posttable. below is my code
<?php
$posts = $wpdb->get_col("
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE post_status = 'publish'
and post_type='post'
ORDER BY post_date DESC LIMIT 10");
$the_posts = array();
foreach($posts as $post) :
echo implode( ', ', $the_posts );
?>
You can do it this way:
1 - get posts (correct way for wp)
<?php
$args = array(
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
2 - loop the posts and get categories
foreach($posts_array as $post):setup_postdata($post);
$category = get_the_category();
var_dump($category);
endforeach;
wp_reset_postdata();

How to display all posts category wise of a custom post type

I am working on a institute website which as courses.
I want to display all posts as list category wise of a custom post type.
for Ex.:
CATEGORY NAME1
- Post of category name 1
- Post of category name 1
CATEGORY NAME2
- Post of category name 2
- Post of category name 2
and so on
below is the code which is displaying all posts from custom post type from all categories but i want to show them category wise seperately please help me...
<?php
$type = 'course';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1);
$my_query = '';
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
?>
you may need to do your query more than once, or one per category the key is adding 'category_name' => 'slug_name' to your query
<?php
// query category 1
$type = 'course';
$args1=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
'caller_get_posts'=> 1);
// query category 2
$type = 'course';
$args2=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
'caller_get_posts'=> 1);
$my_query = '';
$my_query = new WP_Query($args1);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
$my_query = '';
$my_query = new WP_Query($args2);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
?>
100% working this code
$tax_query[] = array(
'taxonomy' => '< taxonomy_slug >',
'field' => 'term_id',
'terms' => < terms_ids >, // array(12,13,...)
);
$args = array(
'post_type' => '< post type name>',
'posts_per_page' => 10,
'tax_query' => $tax_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo get_the_ID();
endwhile;

How To Get Posts start with given letter for custom post type?

I am searching for a solution to get all posts of a custom post type for a while. I found a helpful answer here. But main problem using that snippet is, I want to get posts of a specific custom post type. Normal posts are blog posts so I want to filter those normal posts. I am not good with mysql so asking a solution.
Snippet to get all post of a given letter:
<?php
//get all post IDs for posts beginning with cap B, in title order,
//display posts
$first_char = 'B';
$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $first_char;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
?>
You can select your post type in:
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
by switching the "post_type" value to your custom post type ( not "post" as it is by default ).
for example:
'post_type' => 'customtype',

Wordpress loop inside loop

I have a normal loop that outputs posts based on the given $args.
After three posts I want to insert a post that is from a Featured category. I've tried starting a new WP_Query, simple query_posts in different combinations. Nothing seems to work. Any ideas why ?
$args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'paged' => $paged,
'page' => $paged,
'cat' => $cat,
'ignore_sticky_posts' => 1
);
// create a new instance of WP_Query
$my_query = new WP_Query($args);
<ul>
<?php
$i = 0;
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
if($i == 3): ?>
<li> //insert here one post from featured category
</li>
<?php endif; ?>
<li>
// the normal query stuff is here
</li>
<?php $i++ //post counter
endwhile; //end loop while
endif; //end loop
?>
</ul>
I did it this way. Before the main query I did a special query for the featured post.
$args2 = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category_name' => 'TheCategoryName-Slug',
'ignore_sticky_posts' => 1
);
$my_query2 = new WP_Query($args2);
$post_ids = array(); //create an array to store the ids of the posts
if ($my_query2->have_posts()) : while ($my_query2->have_posts()) : $my_query2->the_post();
$post_ids[] = get_the_ID();
endwhile;
wp_reset_postdata();
endif;
After you have all the ids you can use them to get the data you need. Basically, all the data from the wp_posts table (http://codex.wordpress.org/Database_Description#Table:_wp_posts). The post author is returned as an id that can be used to get the name using get_userdata() function.
echo get_post_field('post_title', $post_ids[0]);
echo get_post_field('post_author', $post_ids[0]);
echo get_post_field('post_date', $post_ids[0]);

Wordpress order posts by custom meta in pagination

I'm ordering my posts by a custom meta value named "size".
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'size'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->postmeta.meta_value DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts):
global $post;
foreach ($pageposts as $post):
setup_postdata($post);
the_title();
endforeach;
endif;
wp_pagenavi(); //creates page navigation
At the same time I'm using the WP-pagenavi plugin to navigate the posts by pages. I have 10 posts on each page.
The problem: Posts are ordered separately in each page. How can I order posts in descending order through all pages?
Update: I might have found a solution but I'm not sure how to implement it in my code
http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
$my_query = new WP_Query( array( 'tag' => 'foo', 'paged' => get_query_var('paged') ) );
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
I've found a SO post that can solve your problem :
How to sort a 'query_posts' function by custom field, while limiting posts by another custom field
There you will find a custom class extending WP_Query and allowing you to make a query ordered by a custom field, and to include the paged query var too.
So the steps :
Past the class PostsOrderedByMetaQuery code somewhere like in your functions.php
Replace your query by :
// Retrieve `paged` in URL
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Make the query like in WP_Query but with our custom class
$query = new PostsOrderedByMetaQuery(array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'orderby_meta_key' => 'size',
'orderby_order' => 'DESC'
));
Use it !
while ( $query->have_posts() ) : $query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $query ) );
wp_reset_postdata(); // avoid errors further down the page

Categories