php – How to I add count of custom posts listed in a post as a prefix to its title


I run an affiliate site that consists primarily of posts that list product ideas (x products for purpose Z).

Each post is a standard post with a custom field ID that is used to generate the list of products from the custom post type “product ideas” that have the same custom field ID. The list is shown right after the_content():

<?php the_content(); ?>
<?php $keywordid = get_field( 'product_id', get_the_ID() );
    $args = array(
        'numberposts'       => -1,
        'post_type'         => 'ideas',
        'post_status'       => 'publish',
        'meta_query' => array(
                'key'       => 'product_id',
                'compare'   => '=',
                'value'     => $keywordid,
        'meta_key'          => 'product_order',
        'orderby'           => 'meta_value_num',
        'order'             => 'ASC',
        'posts_per_page'    =>  500,    

$productideas = get_posts( $args );
if( $productideas ): ?>

    <?php foreach( $productideas as $post ): 
        setup_postdata( $post );
            <h2><li><?php the_field('product_generic_title'); ?></h2>
            <?php the_content(); ?></li>    
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>

<?php endif; ?>

Not sure if there is a more elegant and more performance efficient way, but so far this works.

In addition, though, I would like to add the count of the listed product ideas in the title (across all posts and whereever the titles of the standard posts are shown across the site.

What is the best way to achieve that without having to run multiple queries? The goal is to have a couple of hundreds of those posts with lists of 10 to 20 custom post product ideas listed.

wordpresser 4 weeks 2022-11-06T05:41:35-05:00 0 Answers 0 views 0

Leave an answer