I am pretty sure that the Category parameters cannot be combined with p
. In the documentation it says: “Show posts associated with certain categories.” and none of the examples combine a category with a single post like you are trying to do.
Instead I think you need to use a taxonomy query as such:
$args = [
'post_type' => 'post',
'p'=>(int)$_POST['the_ID'],
'tax_query' => [
[
'taxonomy'=>'category',
'field'=>'slug',
'terms'=>'materials'
]
]
];
Give that a try and remember to mark this as the answer if it solves your problem.
Also, be sure you are getting the right ID. In the code sample you provided, the_ID()
is definitely not what you want. the_ID()
echos the value of the ID to the output. What you probably want instead is get_the_ID()
but even that will be wrong in this context (retrieving a single post by ID as defined by the input coming from the GET request).
And also, if you are plucking the ID from $_POST or $_GET, be sure to sanitize it prior to using it in your query (although, I’m pretty sure WordPress also sanitizes input prior to running the query, you can never be too safe).