#StackBounty: #php #posts #wp-query #functions posts_per_page displays only 2 posts instead of 4 posts

Bounty: 50

I have a wordpress/php code as shown below in which I am trying to pull latest 4 channels at Line A in the code below.

function get_latest_channels( $instance_id = false ) {
    echo $instance_id;
    echo "<br>";
    if ( ! $instance_id ) {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'posts_per_page' => 4,
        );
    } else {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'meta_key'       => 'instance_id',
                'meta_value'     => $instance_id,
                'posts_per_page' => 4,
        );
    }
    $channels = new WP_Query( $query_args );
    if ( $channels->have_posts() ) {
        $rtn = $channels->posts;
    }

    return $rtn;
}

$instance_id = 49;
$latest_channels = HELLOChannelsget_latest_channels($instance_id);
echo "<pre>"; print_r($latest_channels); echo "</pre>". // Line A

The value of $instance_id is 49. On using 49 value, it should pull 4 latest channels.

Problem Statement:

My code is working fine dev/staging server (meaning its pulling latest 4 channels at Line A) but in production server its pulling only 2 channels.

I am wondering what changes I need to make in the php code above so that it pulls latest 4 channels in the production server.

If I comment this line 'posts_per_page' => 4, in the else section of the function get_latest_channels( $instance_id = false ) then Line A is pulling 30 posts in dev server.


Get this bounty!!!

#StackBounty: #php #posts #functions posts_per_page displays only 2 posts instead of 4 posts

Bounty: 50

I have a wordpress/php code as shown below in which I am trying to pull latest 4 channels at Line A in the code below.

function get_latest_channels( $instance_id = false ) {
    echo $instance_id;
    echo "<br>";
    if ( ! $instance_id ) {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'posts_per_page' => 4,
        );
    } else {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'meta_key'       => 'instance_id',
                'meta_value'     => $instance_id,
                'posts_per_page' => 4,
        );
    }
    $channels = new WP_Query( $query_args );
    if ( $channels->have_posts() ) {
        $rtn = $channels->posts;
    }

    return $rtn;
}

$instance_id = 49;
$latest_channels = HELLOChannelsget_latest_channels($instance_id);
echo "<pre>"; print_r($latest_channels); echo "</pre>". // Line A

The value of $instance_id is 49. On using 49 value, it should pull 4 latest channels.

Problem Statement:

My code is working fine dev/staging server (meaning its pulling latest 4 channels at Line A) but in production server its pulling only 2 channels.

I am wondering what changes I need to make in the php code above so that it pulls latest 4 channels in the production server.

If I comment this line 'posts_per_page' => 4, in the else section of the function get_latest_channels( $instance_id = false ) then Line A is pulling 30 posts in dev server.


Get this bounty!!!

#StackBounty: #posts #functions #rest-api #security #json Disable REST API for a user ROLE

Bounty: 50

I’m looking for a way to disable Rest API for a user role called ‘external_user’ (disable wp-json queries.)

This user role can see right now alot of posts and pages information with when we put wp-json on the URL (users, pages, posts…)

I use actually the plugin DISABLE REST API but it prevent only not logged users to see json informations. i need to do the same thing with external_user role.

If it’s not possible, can I redirect this user role (and only external_user role) to 404 pages if he try to put an URL with wp-json ?

Thanks.


Get this bounty!!!

#StackBounty: #posts #pagination Unwanted Duplicate Posts Showing in Pagination Pages

Bounty: 100

For some reason, I have posts that keep repeating on certain pagination pages using the code below. It’s completely random, so I have no idea how to fix the problem. Maybe it’s something in the post itself that’s causing it to be repeated?

Some examples of the problem can be found here:

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>   

<div class="blogcontent"> 
    <div class="post_img"><a href="<?php the_permalink(); ?>">
    <?php $img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
    <?php $image_alt = get_post_meta( get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true); ?>
                <?php if($img) { ?>
                    <img src="<?php echo $img; ?>" alt="<?php echo $image_alt; ?>">
                <?php } ?></a></div>
    <div class="post_content">
    <?php 
        $category_detail=get_the_category($post->ID);//
            foreach($category_detail as $cd){ ?>

                <a href="<?php echo get_home_url(); ?>/category/<?php echo $cd->slug; ?>/"><span class="boldtag"><?php echo $cd->cat_name; ?></span></a>
        <?php       }
    ?>

        <h1><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
        <?php the_excerpt(); ?>
        <ul class="author clearfix">
            <li>By <em><?php the_author_posts_link(); ?></em> <span>ยท <?php the_time('M d, Y') ?></span></li>
            <li><a href="<?php the_permalink(); ?>">MORE...</a></li>
        </ul>
    </div>
</div>
<?php endwhile; ?>

<div class="paginationsec">
    <?php // Previous/next page navigation.
        the_posts_pagination( array(
            'prev_text'          => __( '<i class="fa fa-angle-double-left"></i> Prev', 'twentyfifteen' ),
            'next_text'          => __( 'Next <i class="fa fa-angle-double-right"></i>', 'twentyfifteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentyfifteen' ) . ' </span>',
        ) ); ?>

</div>

<?php else: endif; ?>


Get this bounty!!!

#StackBounty: #posts #wp-query #mysql #join-tables Modify query with complex order

Bounty: 50

I create 2 custom database tables and 1 custom post type. After post creation i fill custom databases with data. This relation holds shop data. wp_post hold main data (mainly title) and connect with terms and first custom table (wp_shop_data) holds all important data of the shop and the second table (wp_shop_openings) hold opening hours.
Below is wp_shop_openings structure:

enter image description here

And current query is modify to this one:

SELECT SQL_CALC_FOUND_ROWS DISTINCT(ppo.shop_id), ppo.day, ppo.open, ppo.close FROM o13k4_posts p
LEFT JOIN o13k4_term_relationships otr ON (p.ID = otr.object_id) 
LEFT JOIN o13k4_shop_openings ppo ON (p.ID = ppo.shop_id) 
WHERE 1=1 
    AND ( otr.term_taxonomy_id IN (2295) ) 
    AND p.post_type = 'cpt_name' 
    AND (p.post_status = 'publish' OR p.post_status = 'acf-disabled' OR p.post_status = 'private') 
GROUP BY ppo.shop_id, ppo.day, 
    ppo.open, 
    ppo.close 
ORDER BY FIELD(ppo.day, 7, 1, 2, 3, 4, 5, 6) DESC, 
    '15:15' COLLATE utf8mb4_unicode_520_ci NOT BETWEEN CONCAT( LPAD( ppo.open, 2, '0'), ':00') AND CONCAT( LPAD( ppo.close, 2, '0'), ':00'), 
    LPAD( ppo.close, 2, '0' ), 
    LPAD( ppo.open, 2, '0' ) 
    LIMIT 0, 2000

But my main problem is that this not return unique results because of the join table “_openings” which order by day and get all days and I don’t want to and I don’t have idea how to construct proper query to get a distinct rows.

What I want is get unique results ordered by current day, than by opening hour. So if shop is open 6 days a week I want to have it only one time in results array. Looking at above db dump there is 6 shops and I want to have an array with 6 element order by day so if today is monday it’s no 1.


Get this bounty!!!