I'm trying to create a shortcode that runs a custom query based on Yoast's primary category meta key.
The goal is to pull posts with the same meta key value of the post being displayed and show 4 more in the sidebar.
So far, everything is working but the query isn't getting posts with the same value.
I added an echo of the meta key value to make sure it was getting it, and it is.
Here's my code:
function primary_category_query_shortcode( $atts ) {
$atts = shortcode_atts( $defaults, $atts, 'primary_category_query' );
$my_meta = get_post_meta ( $id, '_yoast_wpseo_primary_category', true);
// WP_Query arguments
$args = array (
'posts_per_page' => '4',
'meta_query' => array(
array(
'key' => '_yoast_wpseo_primary_category',
'value' => '$my_meta',
),
),
);
echo get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</article>
<?php endwhile; ?>
</section>
<?php } else {
echo 'Sorry, no more found.';
}
/* Restore original Post Data */
wp_reset_postdata();
}
?>```
I'm trying to create a shortcode that runs a custom query based on Yoast's primary category meta key.
The goal is to pull posts with the same meta key value of the post being displayed and show 4 more in the sidebar.
So far, everything is working but the query isn't getting posts with the same value.
I added an echo of the meta key value to make sure it was getting it, and it is.
Here's my code:
function primary_category_query_shortcode( $atts ) {
$atts = shortcode_atts( $defaults, $atts, 'primary_category_query' );
$my_meta = get_post_meta ( $id, '_yoast_wpseo_primary_category', true);
// WP_Query arguments
$args = array (
'posts_per_page' => '4',
'meta_query' => array(
array(
'key' => '_yoast_wpseo_primary_category',
'value' => '$my_meta',
),
),
);
echo get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</article>
<?php endwhile; ?>
</section>
<?php } else {
echo 'Sorry, no more found.';
}
/* Restore original Post Data */
wp_reset_postdata();
}
?>```
Share
Improve this question
edited Apr 17, 2020 at 22:05
Christina
asked Apr 17, 2020 at 21:19
ChristinaChristina
196 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 1Based on our chat in the comments here's what I came with. Please advise if it works.
<?php
function primary_category_query_shortcode( $post, $atts ) {
//global $post; //Uncomment this global if it still doesn't work.
$atts = shortcode_atts( $defaults, $atts, 'primary_category_query' );
$my_meta = get_post_meta ( $post->ID, '_yoast_wpseo_primary_category', true );
// WP_Query arguments
$args = array (
'posts_per_page' => '4',
'meta_query' => array(
array(
'key' => '_yoast_wpseo_primary_category',
'value' => $my_meta
),
),
);
// All this would do is echo the ID of the category, correct? Just for testing?
//echo get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) { ?>
<section class="recent-posts clear">
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'left' ); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</article>
<?php endwhile; ?>
</section>
<?php } else {
echo 'Sorry, no more found.';
}
/* Restore original Post Data */
wp_reset_postdata();
}
?>
An explanation, what I suspect may be happening is that your attempt to get the Yoast Primary Category isn't succeeding because, based on your var_dump() you're not getting the post ID, it's coming back as NULL
. So instead we'll include the $post in the function()
and if that still doesn't work, we'll use global $post;
to make sure it's being referenced. (That would be the second line, just uncomment it if it doesn't work without it.)
I've also commented out the echo get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true );
because that would, in theory, just echo out the ID number of the category. So I assumed you had that in there for testing.
_yoast_wpseo_primary_category
is being assigned to posts? Also, above this lineecho get_post_meta( get_the_ID(), '_yoast_wpseo_primary_category', true);
can you add avar_dump( $id );
and beneath that linevar_dump( $my_meta );
so we can verify what's there? Want to make sure neither of those are empty. – Tony Djukic Commented Apr 18, 2020 at 0:00'$my_meta'
into a string, you want to use the variable direct:'value' => $my_meta
– Michael Commented Apr 18, 2020 at 2:02