Question

I have a profile page on which i want to show all posts with a the tag xy, that is specified in a custom field. I'm having trouble handing the String, which i got from the field over to the wp-query, which gives me the posts with the tag.

I have currently this to setup my query:

$original_query = $wp_query;
$tagstr = '' + the_field('usertag');
$wp_query = null;
$wp_query = new WP_Query( 'tag=$tagstr');

The akward thing is, that it outputs the String of the field itself onto the page sourcecode. I have no echo of the $tagstr variable and am not getting the value of the “usertag” field anywhere else on the page whatsoever, so i absolutely don’t know where this is coming from.

The funny thing is it works fine if i hardcode the String into my code like this:

$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( 'tag=test');

I’m really confused about what is going wrong there…

Was it helpful?

Solution

So the problem was that WP_Query uses a literal string

'tag=$tagstr' 

and the variable was not being converted. I solved it by setting it into double quotes like:

"tag=$tagstr"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top