add filter not working when cancatenating variables

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





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







up vote
1
down vote

favorite












I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;



 $posts_per_page = 'posts_per_page=3';


but it doesn't work if i use concatenation.e.g



 $posts = "posts_per_page=";
$number = 3;
$posts_per_page = $posts. $number;


my full code looks like .



 function blogs() ?>
<article>

<?php // Display blog posts on any page @ https://m0n.co/l
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$posts = 'post_per_page=';
$number = 3;
$posts_per_page = $posts . $number ;
$wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php openblogger_posted_on();
openblogger_posted_by(); ?>

<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

</article>
<?php



Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
Thanks in advance.







share|improve this question


























    up vote
    1
    down vote

    favorite












    I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;



     $posts_per_page = 'posts_per_page=3';


    but it doesn't work if i use concatenation.e.g



     $posts = "posts_per_page=";
    $number = 3;
    $posts_per_page = $posts. $number;


    my full code looks like .



     function blogs() ?>
    <article>

    <?php // Display blog posts on any page @ https://m0n.co/l
    $temp = $wp_query;
    $wp_query= null;
    $wp_query = new WP_Query();
    $posts = 'post_per_page=';
    $number = 3;
    $posts_per_page = $posts . $number ;
    $wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
    <?php openblogger_posted_on();
    openblogger_posted_by(); ?>

    <?php endwhile; ?>

    <?php wp_reset_postdata(); ?>

    </article>
    <?php



    Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
    Thanks in advance.







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;



       $posts_per_page = 'posts_per_page=3';


      but it doesn't work if i use concatenation.e.g



       $posts = "posts_per_page=";
      $number = 3;
      $posts_per_page = $posts. $number;


      my full code looks like .



       function blogs() ?>
      <article>

      <?php // Display blog posts on any page @ https://m0n.co/l
      $temp = $wp_query;
      $wp_query= null;
      $wp_query = new WP_Query();
      $posts = 'post_per_page=';
      $number = 3;
      $posts_per_page = $posts . $number ;
      $wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
      while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

      <h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
      <?php openblogger_posted_on();
      openblogger_posted_by(); ?>

      <?php endwhile; ?>

      <?php wp_reset_postdata(); ?>

      </article>
      <?php



      Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
      Thanks in advance.







      share|improve this question












      I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;



       $posts_per_page = 'posts_per_page=3';


      but it doesn't work if i use concatenation.e.g



       $posts = "posts_per_page=";
      $number = 3;
      $posts_per_page = $posts. $number;


      my full code looks like .



       function blogs() ?>
      <article>

      <?php // Display blog posts on any page @ https://m0n.co/l
      $temp = $wp_query;
      $wp_query= null;
      $wp_query = new WP_Query();
      $posts = 'post_per_page=';
      $number = 3;
      $posts_per_page = $posts . $number ;
      $wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
      while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

      <h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
      <?php openblogger_posted_on();
      openblogger_posted_by(); ?>

      <?php endwhile; ?>

      <?php wp_reset_postdata(); ?>

      </article>
      <?php



      Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
      Thanks in advance.









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 12 at 3:09









      Raashid Din

      155




      155




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Look at your filter:



          apply_filters('change_number_of_posts_home', $posts_per_page)


          The value being passed to it is $posts_per_page, so of course they can't change $number.



          If you want only the number to be filtered then that's the part that should have the filter applied:



          $number = apply_filters('change_number_of_posts_home', 3 );


          Then the query can just look like this:



          $wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);


          They're not relevant to the question, but some other things I noticed about your code:



          • You shouldn't be overwriting $wp_query like that. Just use a new variable for your custom query.

          • You can pass the arguments to new WP_Query( $args ) rather than needing to run ->query() as a separate step.

          • The code would be easier to read if you pass the arguments as an array, rather than a query string.

          • Each individual post should be an <article>. You're wrapping all the posts in a single article tag.





          share|improve this answer




















          • can you please send me a code in which i can write an individual article rather than wrapping
            – Raashid Din
            Aug 12 at 3:38










          • ? Just move the tags so they're inside the loop.
            – Jacob Peattie
            Aug 12 at 3:45










          • can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
            – Raashid Din
            Aug 12 at 3:54










          • now i got you you were talking of article tags
            – Raashid Din
            Aug 12 at 3:56










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "110"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f311183%2fadd-filter-not-working-when-cancatenating-variables%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          Look at your filter:



          apply_filters('change_number_of_posts_home', $posts_per_page)


          The value being passed to it is $posts_per_page, so of course they can't change $number.



          If you want only the number to be filtered then that's the part that should have the filter applied:



          $number = apply_filters('change_number_of_posts_home', 3 );


          Then the query can just look like this:



          $wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);


          They're not relevant to the question, but some other things I noticed about your code:



          • You shouldn't be overwriting $wp_query like that. Just use a new variable for your custom query.

          • You can pass the arguments to new WP_Query( $args ) rather than needing to run ->query() as a separate step.

          • The code would be easier to read if you pass the arguments as an array, rather than a query string.

          • Each individual post should be an <article>. You're wrapping all the posts in a single article tag.





          share|improve this answer




















          • can you please send me a code in which i can write an individual article rather than wrapping
            – Raashid Din
            Aug 12 at 3:38










          • ? Just move the tags so they're inside the loop.
            – Jacob Peattie
            Aug 12 at 3:45










          • can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
            – Raashid Din
            Aug 12 at 3:54










          • now i got you you were talking of article tags
            – Raashid Din
            Aug 12 at 3:56














          up vote
          3
          down vote



          accepted










          Look at your filter:



          apply_filters('change_number_of_posts_home', $posts_per_page)


          The value being passed to it is $posts_per_page, so of course they can't change $number.



          If you want only the number to be filtered then that's the part that should have the filter applied:



          $number = apply_filters('change_number_of_posts_home', 3 );


          Then the query can just look like this:



          $wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);


          They're not relevant to the question, but some other things I noticed about your code:



          • You shouldn't be overwriting $wp_query like that. Just use a new variable for your custom query.

          • You can pass the arguments to new WP_Query( $args ) rather than needing to run ->query() as a separate step.

          • The code would be easier to read if you pass the arguments as an array, rather than a query string.

          • Each individual post should be an <article>. You're wrapping all the posts in a single article tag.





          share|improve this answer




















          • can you please send me a code in which i can write an individual article rather than wrapping
            – Raashid Din
            Aug 12 at 3:38










          • ? Just move the tags so they're inside the loop.
            – Jacob Peattie
            Aug 12 at 3:45










          • can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
            – Raashid Din
            Aug 12 at 3:54










          • now i got you you were talking of article tags
            – Raashid Din
            Aug 12 at 3:56












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Look at your filter:



          apply_filters('change_number_of_posts_home', $posts_per_page)


          The value being passed to it is $posts_per_page, so of course they can't change $number.



          If you want only the number to be filtered then that's the part that should have the filter applied:



          $number = apply_filters('change_number_of_posts_home', 3 );


          Then the query can just look like this:



          $wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);


          They're not relevant to the question, but some other things I noticed about your code:



          • You shouldn't be overwriting $wp_query like that. Just use a new variable for your custom query.

          • You can pass the arguments to new WP_Query( $args ) rather than needing to run ->query() as a separate step.

          • The code would be easier to read if you pass the arguments as an array, rather than a query string.

          • Each individual post should be an <article>. You're wrapping all the posts in a single article tag.





          share|improve this answer












          Look at your filter:



          apply_filters('change_number_of_posts_home', $posts_per_page)


          The value being passed to it is $posts_per_page, so of course they can't change $number.



          If you want only the number to be filtered then that's the part that should have the filter applied:



          $number = apply_filters('change_number_of_posts_home', 3 );


          Then the query can just look like this:



          $wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);


          They're not relevant to the question, but some other things I noticed about your code:



          • You shouldn't be overwriting $wp_query like that. Just use a new variable for your custom query.

          • You can pass the arguments to new WP_Query( $args ) rather than needing to run ->query() as a separate step.

          • The code would be easier to read if you pass the arguments as an array, rather than a query string.

          • Each individual post should be an <article>. You're wrapping all the posts in a single article tag.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 12 at 3:22









          Jacob Peattie

          12.2k41525




          12.2k41525











          • can you please send me a code in which i can write an individual article rather than wrapping
            – Raashid Din
            Aug 12 at 3:38










          • ? Just move the tags so they're inside the loop.
            – Jacob Peattie
            Aug 12 at 3:45










          • can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
            – Raashid Din
            Aug 12 at 3:54










          • now i got you you were talking of article tags
            – Raashid Din
            Aug 12 at 3:56
















          • can you please send me a code in which i can write an individual article rather than wrapping
            – Raashid Din
            Aug 12 at 3:38










          • ? Just move the tags so they're inside the loop.
            – Jacob Peattie
            Aug 12 at 3:45










          • can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
            – Raashid Din
            Aug 12 at 3:54










          • now i got you you were talking of article tags
            – Raashid Din
            Aug 12 at 3:56















          can you please send me a code in which i can write an individual article rather than wrapping
          – Raashid Din
          Aug 12 at 3:38




          can you please send me a code in which i can write an individual article rather than wrapping
          – Raashid Din
          Aug 12 at 3:38












          ? Just move the tags so they're inside the loop.
          – Jacob Peattie
          Aug 12 at 3:45




          ? Just move the tags so they're inside the loop.
          – Jacob Peattie
          Aug 12 at 3:45












          can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
          – Raashid Din
          Aug 12 at 3:54




          can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
          – Raashid Din
          Aug 12 at 3:54












          now i got you you were talking of article tags
          – Raashid Din
          Aug 12 at 3:56




          now i got you you were talking of article tags
          – Raashid Din
          Aug 12 at 3:56












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f311183%2fadd-filter-not-working-when-cancatenating-variables%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Carbon dioxide

          Why am i infinitely getting the same tweet with the Twitter Search API?