How can I use PHP variables within a WP_Query array?How can I prevent SQL injection in PHP?Deleting an element from an array in PHPHow do you check if a variable is an array in JavaScript?How do I get PHP errors to display?How to determine if variable is 'undefined' or 'null'?How do you parse and process HTML/XML in PHP?How to check if a variable is set in Bash?“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHPHow does PHP 'foreach' actually work?Wordpress: Using post__not_in to exclude custom taxonomy category

How to compare two different formulations of a problem?

Can you feel passing through the sound barrier in an F-16?

Why don't electrons take the shorter path in coils

What professions would a medieval village with a population of 100 need?

Defense against attacks using dictionaries

Does an object count as "being moved" when placed in a Bag of Holding before its wielder moves, and then after moving they take the object out again?

How is "sein" conjugated in this sub-sentence?

If all stars rotate, why was there a theory developed, that requires non-rotating stars?

In the MCU, why does Mjölnir retain its enchantments after Ragnarok?

Patching SQL Server 2014 Versus SQL Server 2014 Express

Why we don't have vaccination against all diseases which are caused by microbes?

A square inside an equilateral triangle

Can pay be witheld for hours cleaning up after closing time?

Why is my Earth simulation slower than the reality?

Is it appropriate for a prospective landlord to ask me for my credit report?

Why is less being run unnecessarily by git?

Efficiently pathfinding many flocking enemies around obstacles

Is it possible to create a golf ball sized star?

LeetCode: Pascal's Triangle C#

Script that helps people make better choices

When translating the law, who ensures that the wording does not change the meaning of the law?

How to write triplets in 4/4 time without using a 3 on top of the notes all the time

Is refusing to concede in the face of an unstoppable Nexus combo punishable?

In what ways can a Non-paladin access Paladin spells?



How can I use PHP variables within a WP_Query array?


How can I prevent SQL injection in PHP?Deleting an element from an array in PHPHow do you check if a variable is an array in JavaScript?How do I get PHP errors to display?How to determine if variable is 'undefined' or 'null'?How do you parse and process HTML/XML in PHP?How to check if a variable is set in Bash?“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHPHow does PHP 'foreach' actually work?Wordpress: Using post__not_in to exclude custom taxonomy category






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm trying to pull variables into a WP_Query array, but the intended output isn't what I would expect.



Here's my code:



<?php
$country_name = the_title();
$populate_jhcarousel = new WP_Query( array(
'post_type' => 'story',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'name',
'terms' => "'"$country_name"'"
),
),
'posts_per_page' => 20,
'order'=> 'DESC'
) );
?>


My intention is to bring in all posts whose location is the title of whatever country page their on. In my local setup, I'm testing with Tanzania. When I use the following 'terms' => 'Tanzania', my slider populates properly. But when I use the variable, it either throws a 500 error, or just outputs the word "Tanzania" in place of my slider. Below is what I've tried, and the results:



'terms' => "'"$country_name"'" = HTTP ERROR 500



'terms' => '$country_name' = "Tanzania"



'terms' => $country_name = "Tanzania"



'terms' => 'Tanzania' = Intended result



Any help on using variables within a WP_Query array would be welcomed. Many thanks in advance!!!!










share|improve this question
























  • you get a htrp 500 error because you need to add concatenation operator "." (dot).

    – Dexter0015
    Mar 27 at 16:30











  • more accurately, he needs to realize the quotes and concat are not necessary. just do 'terms' => $country_name

    – Kai Qing
    Mar 27 at 16:42











  • @Dexter0015 would you mind elaborating on your solution? I've tried 'terms' => .$country_name. and I've tried 'terms' => '.$country_name.' and finally 'terms' => "'".$country_name."'" all without my intended result. @KaiQing I've also tried your suggestion, as I stated above.

    – Alex Robinson
    Mar 27 at 18:05











  • @AlexRobinson In the op, you wrote that you used 'terms' => "'"$country_name"'", which is missing the concatenation dots between the double quotes and $country_name. As @KaiQing said, you don't need quotes. If $country_name is a string, it should work. Try outputting and checking that $country_name is correctly set before hand.

    – Daniel Foust
    Mar 27 at 20:03







  • 2





    It looks like the issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title() and see if that works.

    – Daniel Foust
    Mar 27 at 20:07

















0















I'm trying to pull variables into a WP_Query array, but the intended output isn't what I would expect.



Here's my code:



<?php
$country_name = the_title();
$populate_jhcarousel = new WP_Query( array(
'post_type' => 'story',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'name',
'terms' => "'"$country_name"'"
),
),
'posts_per_page' => 20,
'order'=> 'DESC'
) );
?>


My intention is to bring in all posts whose location is the title of whatever country page their on. In my local setup, I'm testing with Tanzania. When I use the following 'terms' => 'Tanzania', my slider populates properly. But when I use the variable, it either throws a 500 error, or just outputs the word "Tanzania" in place of my slider. Below is what I've tried, and the results:



'terms' => "'"$country_name"'" = HTTP ERROR 500



'terms' => '$country_name' = "Tanzania"



'terms' => $country_name = "Tanzania"



'terms' => 'Tanzania' = Intended result



Any help on using variables within a WP_Query array would be welcomed. Many thanks in advance!!!!










share|improve this question
























  • you get a htrp 500 error because you need to add concatenation operator "." (dot).

    – Dexter0015
    Mar 27 at 16:30











  • more accurately, he needs to realize the quotes and concat are not necessary. just do 'terms' => $country_name

    – Kai Qing
    Mar 27 at 16:42











  • @Dexter0015 would you mind elaborating on your solution? I've tried 'terms' => .$country_name. and I've tried 'terms' => '.$country_name.' and finally 'terms' => "'".$country_name."'" all without my intended result. @KaiQing I've also tried your suggestion, as I stated above.

    – Alex Robinson
    Mar 27 at 18:05











  • @AlexRobinson In the op, you wrote that you used 'terms' => "'"$country_name"'", which is missing the concatenation dots between the double quotes and $country_name. As @KaiQing said, you don't need quotes. If $country_name is a string, it should work. Try outputting and checking that $country_name is correctly set before hand.

    – Daniel Foust
    Mar 27 at 20:03







  • 2





    It looks like the issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title() and see if that works.

    – Daniel Foust
    Mar 27 at 20:07













0












0








0








I'm trying to pull variables into a WP_Query array, but the intended output isn't what I would expect.



Here's my code:



<?php
$country_name = the_title();
$populate_jhcarousel = new WP_Query( array(
'post_type' => 'story',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'name',
'terms' => "'"$country_name"'"
),
),
'posts_per_page' => 20,
'order'=> 'DESC'
) );
?>


My intention is to bring in all posts whose location is the title of whatever country page their on. In my local setup, I'm testing with Tanzania. When I use the following 'terms' => 'Tanzania', my slider populates properly. But when I use the variable, it either throws a 500 error, or just outputs the word "Tanzania" in place of my slider. Below is what I've tried, and the results:



'terms' => "'"$country_name"'" = HTTP ERROR 500



'terms' => '$country_name' = "Tanzania"



'terms' => $country_name = "Tanzania"



'terms' => 'Tanzania' = Intended result



Any help on using variables within a WP_Query array would be welcomed. Many thanks in advance!!!!










share|improve this question














I'm trying to pull variables into a WP_Query array, but the intended output isn't what I would expect.



Here's my code:



<?php
$country_name = the_title();
$populate_jhcarousel = new WP_Query( array(
'post_type' => 'story',
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'name',
'terms' => "'"$country_name"'"
),
),
'posts_per_page' => 20,
'order'=> 'DESC'
) );
?>


My intention is to bring in all posts whose location is the title of whatever country page their on. In my local setup, I'm testing with Tanzania. When I use the following 'terms' => 'Tanzania', my slider populates properly. But when I use the variable, it either throws a 500 error, or just outputs the word "Tanzania" in place of my slider. Below is what I've tried, and the results:



'terms' => "'"$country_name"'" = HTTP ERROR 500



'terms' => '$country_name' = "Tanzania"



'terms' => $country_name = "Tanzania"



'terms' => 'Tanzania' = Intended result



Any help on using variables within a WP_Query array would be welcomed. Many thanks in advance!!!!







php wordpress variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 16:26









Alex RobinsonAlex Robinson

286 bronze badges




286 bronze badges















  • you get a htrp 500 error because you need to add concatenation operator "." (dot).

    – Dexter0015
    Mar 27 at 16:30











  • more accurately, he needs to realize the quotes and concat are not necessary. just do 'terms' => $country_name

    – Kai Qing
    Mar 27 at 16:42











  • @Dexter0015 would you mind elaborating on your solution? I've tried 'terms' => .$country_name. and I've tried 'terms' => '.$country_name.' and finally 'terms' => "'".$country_name."'" all without my intended result. @KaiQing I've also tried your suggestion, as I stated above.

    – Alex Robinson
    Mar 27 at 18:05











  • @AlexRobinson In the op, you wrote that you used 'terms' => "'"$country_name"'", which is missing the concatenation dots between the double quotes and $country_name. As @KaiQing said, you don't need quotes. If $country_name is a string, it should work. Try outputting and checking that $country_name is correctly set before hand.

    – Daniel Foust
    Mar 27 at 20:03







  • 2





    It looks like the issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title() and see if that works.

    – Daniel Foust
    Mar 27 at 20:07

















  • you get a htrp 500 error because you need to add concatenation operator "." (dot).

    – Dexter0015
    Mar 27 at 16:30











  • more accurately, he needs to realize the quotes and concat are not necessary. just do 'terms' => $country_name

    – Kai Qing
    Mar 27 at 16:42











  • @Dexter0015 would you mind elaborating on your solution? I've tried 'terms' => .$country_name. and I've tried 'terms' => '.$country_name.' and finally 'terms' => "'".$country_name."'" all without my intended result. @KaiQing I've also tried your suggestion, as I stated above.

    – Alex Robinson
    Mar 27 at 18:05











  • @AlexRobinson In the op, you wrote that you used 'terms' => "'"$country_name"'", which is missing the concatenation dots between the double quotes and $country_name. As @KaiQing said, you don't need quotes. If $country_name is a string, it should work. Try outputting and checking that $country_name is correctly set before hand.

    – Daniel Foust
    Mar 27 at 20:03







  • 2





    It looks like the issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title() and see if that works.

    – Daniel Foust
    Mar 27 at 20:07
















you get a htrp 500 error because you need to add concatenation operator "." (dot).

– Dexter0015
Mar 27 at 16:30





you get a htrp 500 error because you need to add concatenation operator "." (dot).

– Dexter0015
Mar 27 at 16:30













more accurately, he needs to realize the quotes and concat are not necessary. just do 'terms' => $country_name

– Kai Qing
Mar 27 at 16:42





more accurately, he needs to realize the quotes and concat are not necessary. just do 'terms' => $country_name

– Kai Qing
Mar 27 at 16:42













@Dexter0015 would you mind elaborating on your solution? I've tried 'terms' => .$country_name. and I've tried 'terms' => '.$country_name.' and finally 'terms' => "'".$country_name."'" all without my intended result. @KaiQing I've also tried your suggestion, as I stated above.

– Alex Robinson
Mar 27 at 18:05





@Dexter0015 would you mind elaborating on your solution? I've tried 'terms' => .$country_name. and I've tried 'terms' => '.$country_name.' and finally 'terms' => "'".$country_name."'" all without my intended result. @KaiQing I've also tried your suggestion, as I stated above.

– Alex Robinson
Mar 27 at 18:05













@AlexRobinson In the op, you wrote that you used 'terms' => "'"$country_name"'", which is missing the concatenation dots between the double quotes and $country_name. As @KaiQing said, you don't need quotes. If $country_name is a string, it should work. Try outputting and checking that $country_name is correctly set before hand.

– Daniel Foust
Mar 27 at 20:03






@AlexRobinson In the op, you wrote that you used 'terms' => "'"$country_name"'", which is missing the concatenation dots between the double quotes and $country_name. As @KaiQing said, you don't need quotes. If $country_name is a string, it should work. Try outputting and checking that $country_name is correctly set before hand.

– Daniel Foust
Mar 27 at 20:03





2




2





It looks like the issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title() and see if that works.

– Daniel Foust
Mar 27 at 20:07





It looks like the issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title() and see if that works.

– Daniel Foust
Mar 27 at 20:07












1 Answer
1






active

oldest

votes


















1













The issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title().






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382108%2fhow-can-i-use-php-variables-within-a-wp-query-array%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1













    The issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title().






    share|improve this answer





























      1













      The issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title().






      share|improve this answer



























        1












        1








        1







        The issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title().






        share|improve this answer













        The issue is that you are using the_title(), which has the default behavior of echoing out the title. Change that to get_the_title().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 20:25









        Daniel FoustDaniel Foust

        3771 gold badge3 silver badges16 bronze badges




        3771 gold badge3 silver badges16 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382108%2fhow-can-i-use-php-variables-within-a-wp-query-array%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript