I need to create a WP_Query
that lists only a specific post of a post type, I already have this in the query, but my problem is that the query filter receives via get the ID, With that suppose I have two posts one with the id=10
and another with the id=100
ends up that only the post from id 10 is listed.
Also with the id is defined via get, I need that if the get send the id with some more letter or number it ignore because today if the query receives the id 10sddds it accepts and displays the post of id 10 I need the query ignore the id consisting of numbers and letters.
// WP_Query arguments
$args = array (
'post_type' => 'file',
'p' => '925s',
'post_status' => 'publish',
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
} else {
// no posts found
get_template_part( 'templates/content/not-found-search' );
}
wp_reset_postdata();
I need to create a WP_Query
that lists only a specific post of a post type, I already have this in the query, but my problem is that the query filter receives via get the ID, With that suppose I have two posts one with the id=10
and another with the id=100
ends up that only the post from id 10 is listed.
Also with the id is defined via get, I need that if the get send the id with some more letter or number it ignore because today if the query receives the id 10sddds it accepts and displays the post of id 10 I need the query ignore the id consisting of numbers and letters.
// WP_Query arguments
$args = array (
'post_type' => 'file',
'p' => '925s',
'post_status' => 'publish',
);
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
} else {
// no posts found
get_template_part( 'templates/content/not-found-search' );
}
wp_reset_postdata();
This is a very unusual way of accomplishing what you wanted
but my problem is that the query filter receives via get the ID, With that suppose I have two posts one with the id=10 and another with the id=100 ends up that only the post from id 10 is listed.
I do not know what you mean here, but I did notice it seems to have nothing in common with your code example. If we look at the query args we see:
$args = array (
'post_type' => 'file',
'p' => '925s',
'post_status' => 'publish',
);
925s
is not a valid post ID, if you want to fetch a single post, it's much easier to do this:
$p = get_post( 925 );
And if you want to fetch multiple posts in a query, we can look up the official docs at https://codex.wordpress/Class_Reference/WP_Query which specifies:
post__in (array) - use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.
Giving us:
$args = [
'post_type' => 'file',
'post__in' => [ 1, 2, 4, etc.. ]
];
All of this is a problem though, you should not be hard coding post IDs. It will break if you ever delete the posts, migrate, move, etc
Use post slugs instead, e.g.
$args = [
'post_type' => 'file',
'post_name__in' => [ 'post1', 'post2', etc.. ]
];