0

Visual Debugging

Here is my code:

global $post;
$args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'post',
);
$the_query = new WP_Query( $args );
$array = get_object_vars($the_query); //convert the object into an array (manual workaround)
if ( $the_query->have_posts() ) :
    $count = 0; //start an iteration count
    while ( $the_query->have_posts() ) : ?>
       <? $the_query->the_post(); ?>
       <?php $postarray = get_object_vars($array['posts'][$count]); 
    //convert the post object into an array
  ?> 
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <h4><?php echo get_the_title(); ?></h4>
                    <p><?php echo get_the_excerpt(); ?></p>
                    <?php the_permalink() ?>
                <!-- the guid is now accessable -->
                    <a href="<?php echo $postarray['guid']; ?>">Read More</a>
                </div>
            </div>
        </div>
    <?  endwhile;
else :
    echo wpautop( 'Sorry, no posts were found' );
endif;

I have referenced other questions about this, however my global $post; IS outside the loop (as referenced from Post Loop not Returning Permalink) and I have also already tried using the range of functions for grabbing/showing the permalink and guid value (as referenced from get_the_permalink() Documentation

also its worth noting that get_the_ID() returned the currently viewed page id NOT the iterated post id

also this is all contained within a shortcode function.

any help is greatly appreciated :)

8
  • 1
    So, do you mean that the loop is returning the correct title, but wrong permalink? Also as a side note, never use short php tags <?, always use the full <?php form. – Jack Johansson Aug 11 '17 at 0:38
  • 1
    Can you remove the short tag and verify 100% it has nothing to do with this issue? It's a massive red herring. Can you also verify that nothing is hooked into the permalink filters? – Tom J Nowell Aug 11 '17 at 1:05
  • I found A solution ill amend my answer to demonstrate my solution.. yes the title is correct but the ID permalink and guid were all returning the currently viewed page values – Solorside Aug 11 '17 at 1:06
  • I amended my question to show my (non ideal) solution. If anyone has a better solution feel free – Solorside Aug 11 '17 at 1:13
  • In addition to what @TomJNowell Said, I would also remove the global post and see if it has anything to do with that. – Jack Johansson Aug 11 '17 at 1:19
1

I'm actually surprised that your code is working. There are several typos in it that should be throwing fatal errors.

global $post;
$args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'post',
);
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : ?>
       <?php $the_query->the_post(); 
       $id = get_the_ID(); ?>
        <div class="row">
            <div class="col-md-6">
                <div class="card">
                    <h4><?php echo get_the_title( $id ); ?></h4>
                    <p><?php echo get_the_excerpt( $id ); ?></p>
                    <?php echo get_the_permalink( $id ); ?>

                    <a href="<?php echo get_the_permalink( $id ); ?>">Read More</a>
                </div>
            </div>
        </div>
    <?php  endwhile;
else :
    echo wpautop( 'Sorry, no posts were found' );
endif;

You mentioned that get_the_ID(); function works fine. So, what you can do is to manually feed the permalink function with the ID, something that I've done in my answer.

Also, you have a couple of missing semicolons, which I've corrected in the answer.

As a side note, ever use short PHP tags <?. They are deprecated, and on my system they even throw a fatal error. Always use full <?php tags.

2
  • if you read my post again, you will notice that i wrote get_the_ID() is NOT working.. in caps – Solorside Aug 11 '17 at 1:08
  • i noticed the typo, was a result of playing with different variables. still does not answer why the permalink and the id and the guid will not return in the loop – Solorside Aug 11 '17 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.