Query Posts By Category for Dropdown Option Widget/Sidebar

I’m attempting to create a grid-layout sidebar/widget that allows the user to select specific posts to display in designated places (4 posts in this case). I can’t seem to get any post identification to show in the dropdown.

I have tried get_post, and now I’m moving on to WP_Query, and I guess a direct SQL communication would be my last resort. Help! I’ve been working on this all week..If this can be achieved with get_post, that would be preferable.

function form( $instance ) {
$cat_1 = ! empty( $instance[ ‘cat_1’ ] ) ? $instance[ ‘cat_1’ ] : 0;
$post_1 = ! empty( $instance[ ‘post_1’ ] ) ? $instance[ ‘post_1’ ] : 0;
?>
<p>
<label for=”<?php echo esc_attr( $this->get_field_id( ‘cat_1’ ) )?>”> <strong><?php echo esc_html__( ‘Featured Post – Left:’, ‘JointsWP’ ); ?></strong></label>
</p>
<br>
<?php
$cat_args_1 = array(
‘orderby’ => ‘name’,
‘hide_empty’ => 0,
‘id’ => $this->get_field_id( ‘cat_1’ ),
‘name’ => $this->get_field_name( ‘cat_1’ ),
‘class’ => ‘widefat’,
‘taxonomy’ => ‘category’,
‘selected’ => absint( $cat_1 ),
‘show_option_all’ => esc_html__( ‘Show Recent Post’, ‘JointsWP’ )
);
wp_dropdown_categories( $cat_args_1 );
?>

<?php
global $post // observation – doesn’t seem to do anything in this case
$post_args_1 = array(
‘id’ => $this->get_field_id( ‘post_1’ ),
‘name’ => $this->get_field_name( ‘post_1’ ),
‘class’ => ‘widefat’,
‘cat’ => $cat_1,
‘post_type’ => ‘post’,
);
$post_1 = null;
$post_1 = new WP_Query( $post_args_1 );
?>
<p>
<label for=”<?php echo $this->get_field_id( ‘post_1’ ); ?>”>Choose a
post to display:</label>
<select name=”<?php echo $this->get_field_name( ‘post_1’ ); ?>” id=”<?
php echo $this->get_field_id( ‘post’ ); ?>” class=”widefat”>
<?php foreach( $post_1 as $post ) :
setup_postdata( $post_1->the_post() ); ?> // this also doesn’t seem to do anything??
<option value=”<?php the_permalink(); ?>”>
<?php echo the_title(); ?>
</option>
<?php endforeach;
; ?>
</select>
<?php

wp_reset_postdata(); ?>
</p>

Read more here:: Query Posts By Category for Dropdown Option Widget/Sidebar

Leave a Reply

Your email address will not be published. Required fields are marked *