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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현